internal override IntPtr Callback(int nCode, IntPtr wParam, IntPtr lParam) { var data = (MSLLHOOKSTRUCT)PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var ea = new LowLevelMouseProcEventArgs(data); LowLevelMouseProc?.Invoke((WPARAM)wParam, ea); switch ((WPARAM)wParam) { case WPARAM.WM_MOUSEMOVE: MouseMove?.Invoke(ea); break; case WPARAM.WM_LBUTTONDOWN: LButtonDown?.Invoke(ea); break; case WPARAM.WM_LBUTTONUP: LButtonUp?.Invoke(ea); break; case WPARAM.WM_RBUTTONDOWN: RButtonDown?.Invoke(ea); break; case WPARAM.WM_RBUTTONUP: RButtonUp?.Invoke(ea); break; case WPARAM.WM_MOUSEWHEEL: MouseWheel?.Invoke(ea); break; case WPARAM.WM_MOUSEHWHEEL: MouseHWheel?.Invoke(ea); break; } return(base.Callback(nCode, wParam, lParam)); }
/// <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)); }