protected override void OnMessageReceived(object sender, MessageEventArgs e) { Contract.Requires(e != null); var eventArgs = new MouseMessageEventArgs { MessageCode = BitConverter.ToInt32(e.Bytes, 0), X = BitConverter.ToInt32(e.Bytes, 4), Y = BitConverter.ToInt32(e.Bytes, 8), Handle = BitConverter.ToInt32(e.Bytes, 12), HitTestCode = BitConverter.ToInt32(e.Bytes, 16), Delta = BitConverter.ToInt16(e.Bytes, 20), }; Debug.WriteLine($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Delta: {eventArgs.Delta}"); MessageReceived?.Invoke(this, eventArgs); switch (GetMessageCode(eventArgs.MessageCode)) { case MouseMessageCode.MouseMove: MouseMove?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonDown: LeftButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonUp: LeftButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonDblClk: LeftButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonDown: RightButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonUp: RightButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonDblClk: RightButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonDown: MiddleButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonUp: MiddleButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonDblClk: MiddleButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MouseWheel: MouseWheel?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonDown: XButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonUp: XButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonDblClk: XButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MouseHWheel: MouseHWheel?.Invoke(this, eventArgs); break; default: break; } if (_messageHandlers.ContainsKey(eventArgs.MessageCode)) { _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs); } }
/// <summary> /// The internal hook callback. /// </summary> private IntPtr HookCallback( int nCode, IntPtr wParam, IntPtr lParam) { int nmsg = wParam.ToInt32(); WindowMessage msg = (WindowMessage)nmsg; if (nCode >= 0 && (nmsg >= (int)WindowMessage.WM_MOUSEFIRST && nmsg <= (int)WindowMessage.WM_MOUSEWHEEL)) { try { int vkCode = Marshal.ReadInt32(lParam); Keys key = (Keys)vkCode; if (!NativeMethods.GetCursorPos(out NativeMethods.POINT pt)) { throw new Win32Exception(); } MouseHookEventArgs eventArgs = new MouseHookEventArgs(new Point(pt.X, pt.Y)); switch (msg) { case WindowMessage.WM_MOUSEMOVE: MouseMove?.Invoke(this, eventArgs); break; case WindowMessage.WM_LBUTTONDOWN: LButtonDown?.Invoke(this, eventArgs); break; case WindowMessage.WM_LBUTTONUP: LButtonUp?.Invoke(this, eventArgs); break; case WindowMessage.WM_LBUTTONDBLCLK: LButtonDblClick?.Invoke(this, eventArgs); break; case WindowMessage.WM_RBUTTONDOWN: RButtonDown?.Invoke(this, eventArgs); break; case WindowMessage.WM_RBUTTONUP: RButtonUp?.Invoke(this, eventArgs); break; case WindowMessage.WM_RBUTTONDBLCLK: RButtonDblClick?.Invoke(this, eventArgs); break; case WindowMessage.WM_MBUTTONDOWN: MButtonDown?.Invoke(this, eventArgs); break; case WindowMessage.WM_MBUTTONUP: MButtonUp?.Invoke(this, eventArgs); break; case WindowMessage.WM_MBUTTONDBLCLK: MButtonDblClick?.Invoke(this, eventArgs); break; case WindowMessage.WM_XBUTTONDOWN: XButtonDown?.Invoke(this, eventArgs); break; case WindowMessage.WM_XBUTTONUP: XButtonUp?.Invoke(this, eventArgs); break; case WindowMessage.WM_XBUTTONDBLCLK: XButtonDblClick?.Invoke(this, eventArgs); break; case WindowMessage.WM_MOUSEWHEEL: MouseVWheel?.Invoke(this, eventArgs); break; case WindowMessage.WM_MOUSEHWHEEL: MouseHWheel?.Invoke(this, eventArgs); break; } // If Handled is set to true, // return 1 and stop the keyboard event // from propagating to other applications. if (eventArgs.Handled) { return((IntPtr)1); } } catch (Exception e) { System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { System.Windows.MessageBox.Show(e.ToString(), "Application Error", System.Windows.MessageBoxButton.OK, System.Windows.MessageBoxImage.Error); }); } } return(NativeMethods.CallNextHookEx(_nativeHookId, nCode, wParam, lParam)); }
protected override void OnMessageReceived(object sender, MessageEventArgs e) { Contract.Requires(e != null); var modifiers = BitConverter.ToUInt16(e.Bytes, 24); var eventArgs = new MouseMessageEventArgs { MessageCode = BitConverter.ToInt32(e.Bytes, 0), X = BitConverter.ToInt32(e.Bytes, 4), Y = BitConverter.ToInt32(e.Bytes, 8), Handle = BitConverter.ToInt32(e.Bytes, 12), HitTestCode = BitConverter.ToInt32(e.Bytes, 16), Modifiers = modifiers, Shift = (modifiers & 0b100) > 0, Control = (modifiers & 0b10) > 0, Alt = (modifiers & 0b1) > 0, }; var messageCode = GetMessageCode(eventArgs.MessageCode); if (messageCode == MouseMessageCode.MouseWheel) { eventArgs.Delta = BitConverter.ToInt16(e.Bytes, 20); } else if (messageCode == MouseMessageCode.NCXButtonDown || messageCode == MouseMessageCode.NCXButtonUp || messageCode == MouseMessageCode.NCXButtonDblClk || messageCode == MouseMessageCode.XButtonDown || messageCode == MouseMessageCode.XButtonUp || messageCode == MouseMessageCode.XButtonDblClk) { eventArgs.XButtons = BitConverter.ToInt16(e.Bytes, 20); } Debug.Write($"Code: {eventArgs.MessageCode}; X: {eventArgs.X}; Y: {eventArgs.Y}; Modifiers: {eventArgs.Modifiers:x}; "); Debug.WriteLine($"Delta: {eventArgs.Delta}; XButtons: {eventArgs.XButtons}"); MessageReceived?.Invoke(this, eventArgs); switch (messageCode) { case MouseMessageCode.MouseMove: MouseMove?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonDown: LeftButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonUp: LeftButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.LeftButtonDblClk: LeftButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonDown: RightButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonUp: RightButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.RightButtonDblClk: RightButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonDown: MiddleButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonUp: MiddleButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.MiddleButtonDblClk: MiddleButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MouseWheel: MouseWheel?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonDown: XButtonDown?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonUp: XButtonUp?.Invoke(this, eventArgs); break; case MouseMessageCode.XButtonDblClk: XButtonDblClk?.Invoke(this, eventArgs); break; case MouseMessageCode.MouseHWheel: MouseHWheel?.Invoke(this, eventArgs); break; default: break; } if (_messageHandlers.ContainsKey(eventArgs.MessageCode)) { _messageHandlers[eventArgs.MessageCode]?.Invoke(this, eventArgs); } }