public MouseHookEventArgs(MSLLHOOKSTRUCT data, CURSORINFO info, MouseKey keys, int param, int lastX, int lastY) { _data = data; _info = info; _keys = keys; _Param = param; _LastX = lastX; _LastY = lastY; }
internal static extern bool GetCursorInfo(out CURSORINFO pci);
public static extern bool GetCursorInfo(out CURSORINFO info);
protected override IntPtr HookProcess(int nCode, IntPtr wParam, IntPtr lParam) { if (EnableEvents) { // If OK and someone listens to our events if (nCode >= 0) { var info = new CURSORINFO(); info.Size = Marshal.SizeOf(info.GetType()); if (!GetCursorInfo(out info)) { var ex = new System.ComponentModel.Win32Exception(); throw new Exception(ex.Message); } var button = MouseButtons.None; var param = wParam.ToInt32(); // Marshall the data from callback. var mStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var delta = 0; var tu = new TestUnion { Number = mStruct.mouseData }; MouseKey mk = 0; int lastX = 0; int lastY = 0; bool handled; switch (param) { case WM_MOUSEMOVE: var x = mStruct.pt.x; var y = mStruct.pt.y; if (prevX == -1 || prevY == -1) { scrnX = SystemInformation.VirtualScreen.Width - 1; scrnY = SystemInformation.VirtualScreen.Height - 1; prevX = x; prevY = y; } lastX = x - prevX; lastY = y - prevY; var fX = (int)Math.Max(Math.Min(scrnX, x), 0); var fY = (int)Math.Max(Math.Min(scrnY, y), 0); if (fX != x || fY != y) SetCursorPos(fX, fY); prevX = fX; prevY = fY; break; case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: button = MouseButtons.Left; break; case WM_MBUTTONDOWN: case WM_MBUTTONUP: case WM_MBUTTONDBLCLK: button = MouseButtons.Middle; break; case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_RBUTTONDBLCLK: button = MouseButtons.Right; break; case WM_XBUTTONDOWN: case WM_XBUTTONUP: case WM_XBUTTONDBLCLK: button = MouseButtons.XButton1; if (tu.High == 1) mk = MouseKey.MK_XBUTTON1; if (tu.High == 2) mk = MouseKey.MK_XBUTTON2; break; case WM_NCXBUTTONDOWN: case WM_NCXBUTTONUP: case WM_NCXBUTTONDBLCLK: button = MouseButtons.XButton2; if (tu.High == 1) mk = MouseKey.MK_XBUTTON1; if (tu.High == 2) mk = MouseKey.MK_XBUTTON2; break; case WM_MOUSEWHEEL: case WM_MOUSEHWHEEL: delta = (int)tu.High; mk = (MouseKey)tu.ULow; break; case WM_TOUCH: try { handled = DecodeTouch(wParam, lParam); } catch (Exception ex) { if (OnError != null) OnError(this, new UnhandledExceptionEventArgs(ex, false)); } break; } var ea = new MouseHookEventArgs(mStruct, info, mk, param, lastX, lastY); if (OnMouseHook != null) OnMouseHook(this, ea); int clickCount = 0; if (button != MouseButtons.None) clickCount = (param == WM_LBUTTONDBLCLK || param == WM_RBUTTONDBLCLK) ? 2 : 1; var e = new MouseEventArgs(button, clickCount, mStruct.pt.x, mStruct.pt.y, delta); // Raise Events. if (OnMouseUp != null && (param == WM_LBUTTONUP || param == WM_MBUTTONUP || param == WM_RBUTTONUP || param == WM_XBUTTONUP || param == WM_NCXBUTTONUP)) OnMouseUp(this, e); else if (OnMouseDown != null && (param == WM_LBUTTONDOWN || param == WM_MBUTTONDOWN || param == WM_RBUTTONDOWN || param == WM_XBUTTONDOWN || param == WM_NCXBUTTONDOWN)) OnMouseDown(this, e); else if (OnMouseMove != null && (param == WM_MOUSEMOVE)) OnMouseMove(this, e); else if (OnMouseWheel != null) OnMouseWheel(this, e); else if (OnMouseActivity != null) OnMouseActivity(this, e); } } return CallNextHookEx(hHook, nCode, wParam, lParam); }
protected override IntPtr Hook1Procedure(int nCode, IntPtr wParam, IntPtr lParam) { if (EnableEvents) { // If OK and someone listens to our events if (nCode >= 0) { var info = new CURSORINFO(); info.Size = Marshal.SizeOf(info.GetType()); if (!NativeMethods.GetCursorInfo(out info)) { var ex = new System.ComponentModel.Win32Exception(); throw new Exception(ex.Message); } var button = MouseButtons.None; var param = wParam.ToInt32(); // Marshall the data from callback. var mStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var delta = 0; var tu = new TestUnion(mStruct.mouseData); MouseKey mk = 0; int lastX = 0; int lastY = 0; bool handled; var unknownAction = false; switch (param) { case WM_MOUSEMOVE: var x = mStruct.pt.x; var y = mStruct.pt.y; if (prevX == -1 || prevY == -1) { scrnX = SystemInformation.VirtualScreen.Width - 1; scrnY = SystemInformation.VirtualScreen.Height - 1; prevX = x; prevY = y; } lastX = x - prevX; lastY = y - prevY; var fX = Math.Max(Math.Min(scrnX, x), 0); var fY = Math.Max(Math.Min(scrnY, y), 0); if (fX != x || fY != y) { NativeMethods.SetCursorPos(fX, fY); } prevX = fX; prevY = fY; break; case WM_LBUTTONDOWN: case WM_LBUTTONUP: case WM_LBUTTONDBLCLK: button = MouseButtons.Left; break; case WM_MBUTTONDOWN: case WM_MBUTTONUP: case WM_MBUTTONDBLCLK: button = MouseButtons.Middle; break; case WM_RBUTTONDOWN: case WM_RBUTTONUP: case WM_RBUTTONDBLCLK: button = MouseButtons.Right; break; case WM_XBUTTONDOWN: case WM_XBUTTONUP: case WM_XBUTTONDBLCLK: button = MouseButtons.XButton1; if (tu.High == 1) { mk = MouseKey.MK_XBUTTON1; } if (tu.High == 2) { mk = MouseKey.MK_XBUTTON2; } break; case WM_NCXBUTTONDOWN: case WM_NCXBUTTONUP: case WM_NCXBUTTONDBLCLK: button = MouseButtons.XButton2; if (tu.High == 1) { mk = MouseKey.MK_XBUTTON1; } if (tu.High == 2) { mk = MouseKey.MK_XBUTTON2; } break; case WM_MOUSEWHEEL: case WM_MOUSEHWHEEL: delta = tu.High; mk = (MouseKey)tu.ULow; break; case WM_TOUCH: try { handled = DecodeTouch(wParam, lParam); } catch (Exception ex) { if (OnError != null) { OnError(this, new UnhandledExceptionEventArgs(ex, false)); } } break; default: unknownAction = true; break; } var ea = new MouseHookEventArgs(mStruct, info, mk, param, lastX, lastY, null); if (OnMouseHook != null) { OnMouseHook(this, ea); } int clickCount = 0; if (button != MouseButtons.None) { clickCount = (param == WM_LBUTTONDBLCLK || param == WM_RBUTTONDBLCLK) ? 2 : 1; } var e = new MouseEventArgs(button, clickCount, mStruct.pt.x, mStruct.pt.y, delta); // Raise Events. if (OnMouseUp != null && (param == WM_LBUTTONUP || param == WM_MBUTTONUP || param == WM_RBUTTONUP || param == WM_XBUTTONUP || param == WM_NCXBUTTONUP)) { OnMouseUp(this, e); } else if (OnMouseDown != null && (param == WM_LBUTTONDOWN || param == WM_MBUTTONDOWN || param == WM_RBUTTONDOWN || param == WM_XBUTTONDOWN || param == WM_NCXBUTTONDOWN)) { OnMouseDown(this, e); } else if (OnMouseMove != null && (param == WM_MOUSEMOVE)) { OnMouseMove(this, e); } else if (OnMouseWheel != null) { OnMouseWheel(this, e); } else if (OnMouseActivity != null) { OnMouseActivity(this, e); } if (unknownAction) { var ev = OnMouseUnknown; if (ev != null) { ev(this, ea); } } } } return(NativeMethods.CallNextHookEx(_Hook1Handle, nCode, wParam, lParam)); }