private int HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode != 0) { return(User32.CallNextHookEx(HookType.WH_MOUSE_LL, nCode, wParam, lParam)); } int eventType = (int)wParam; MOUSELLHOOKSTRUCT hookStruct = Marshal.PtrToStructure <MOUSELLHOOKSTRUCT>(lParam); var message = new MouseEventMessage { EventType = eventType, PointX = hookStruct.pt.x, PointY = hookStruct.pt.y, MouseData = hookStruct.mouseData, Flags = hookStruct.flags, Time = hookStruct.time, ExtraInfo = hookStruct.extraInfo }; MouseResponseMessage?response = messageService.SendAndWait <MouseResponseMessage>(message, TimeSpan.FromMilliseconds(CALLBACK_TIMEOUT)); if (response?.IsHandled == true) { return(1); } else { return(User32.CallNextHookEx(HookType.WH_MOUSE_LL, nCode, wParam, lParam)); } }
private static void RaiseMouseMoveEvent(MOUSELLHOOKSTRUCT mouseHookStruct, ChoMouseEventArgs e) { EventHandler <ChoMouseEventArgs> mouseMove = _mouseMove; if (mouseMove == null) { return; } //If someone listens to move and there was a change in coordinates raise move event if (_oldMouseX != mouseHookStruct.Point.X || _oldMouseY != mouseHookStruct.Point.Y) { _oldMouseX = mouseHookStruct.Point.X; _oldMouseY = mouseHookStruct.Point.Y; mouseMove.Raise(null, e); } }
protected override bool HooksCallbackProc(int code, int wParam, IntPtr lParam) { if (code > -1) { //Marshall the data from callback. MOUSELLHOOKSTRUCT mouseHookStruct = (MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSELLHOOKSTRUCT)); MouseButtons button = GetButton(wParam); ChoMouseEventType eventType = GetEventType(wParam); ChoMouseEventArgs e = new ChoMouseEventArgs( button, (eventType == ChoMouseEventType.DoubleClick ? 2 : 1), mouseHookStruct.Point.X, mouseHookStruct.Point.Y, (eventType == ChoMouseEventType.MouseWheel ? (short)((mouseHookStruct.MouseData >> 16) & 0xffff) : 0)); // Prevent multiple Right Click events (this probably happens for popup menus) if (button == MouseButtons.Right && mouseHookStruct.Flags != 0) { eventType = ChoMouseEventType.None; } switch (eventType) { case ChoMouseEventType.MouseDown: _mouseDown.Raise(this, e); break; case ChoMouseEventType.MouseUp: _mouseClick.Raise(this, e); _mouseUp.Raise(this, e); break; case ChoMouseEventType.DoubleClick: _mouseDblClick.Raise(this, e); break; case ChoMouseEventType.MouseWheel: _mouseWheel.Raise(this, e); break; case ChoMouseEventType.MouseMove: //If someone listens to move and there was a change in coordinates raise move event if (_oldMouseX != mouseHookStruct.Point.X || _oldMouseY != mouseHookStruct.Point.Y) { _oldMouseX = mouseHookStruct.Point.X; _oldMouseY = mouseHookStruct.Point.Y; _mouseMove.Raise(this, e); } break; default: break; } return(e.Handled); } return(false); }
private static bool MouseHooksProc(int code, int wParam, IntPtr lParam) { bool handled = false; if (code >= 0) { //Marshall the data from callback. MOUSELLHOOKSTRUCT mouseHookStruct = (MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSELLHOOKSTRUCT)); //detect button clicked MouseButtons button = MouseButtons.None; short mouseDelta = 0; int clickCount = 0; bool mouseDown = false; bool mouseUp = false; switch (wParam) { case ChoUser32.WM_LBUTTONDOWN: mouseDown = true; button = MouseButtons.Left; clickCount = 1; break; case ChoUser32.WM_LBUTTONUP: mouseUp = true; button = MouseButtons.Left; clickCount = 1; break; case ChoUser32.WM_LBUTTONDBLCLK: button = MouseButtons.Left; clickCount = 2; break; case ChoUser32.WM_RBUTTONDOWN: mouseDown = true; button = MouseButtons.Right; clickCount = 1; break; case ChoUser32.WM_RBUTTONUP: mouseUp = true; button = MouseButtons.Right; clickCount = 1; break; case ChoUser32.WM_RBUTTONDBLCLK: button = MouseButtons.Right; clickCount = 2; break; case ChoUser32.WM_MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of MouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, MouseData is not used. break; } //generate event ChoMouseEventArgs e = new ChoMouseEventArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); //Mouse up if (mouseUp) { _mouseUp.Raise(null, e); } handled = handled || e.Handled; //Mouse down if (mouseDown) { _mouseDown.Raise(null, e); } handled = handled || e.Handled; //If someone listens to click and a click is heppened if (clickCount > 0) { _mouseClick.Raise(null, e); } handled = handled || e.Handled; //If someone listens to double click and a click is heppened if (clickCount == 2) { _mouseDblClick.Raise(null, e); } handled = handled || e.Handled; //Wheel was moved if (mouseDelta != 0) { _mouseWheel.Raise(null, e); } handled = handled || e.Handled; RaiseMouseMoveEvent(mouseHookStruct, e); handled = handled || e.Handled; } return(handled); }
/// <summary> /// Processes Mouse Procedures /// </summary> /// <param name="code"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> private IntPtr MouseProc(int code, IntPtr wParam, IntPtr lParam) { MOUSELLHOOKSTRUCT hookStruct = (MOUSELLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MOUSELLHOOKSTRUCT)); int message = wParam.ToInt32(); int x = hookStruct.pt.x; int y = hookStruct.pt.y; int delta = (short)((hookStruct.mouseData >> 16) & 0xffff); if (Messages.WM_MOUSEWHEEL == message) { OnMouseWheel(new MouseEventArgs(MouseButtons.None, 0, x, y, delta)); } else if (Messages.WM_MOUSEMOVE == message) { OnMouseMove(new MouseEventArgs(MouseButtons.None, 0, x, y, delta)); } else if (Messages.WM_LBUTTONDBLCLK == message) { OnMouseDoubleClick(new MouseEventArgs(MouseButtons.Left, 0, x, y, delta)); } else if (Messages.WM_LBUTTONDOWN == message) { OnMouseDown(new MouseEventArgs(MouseButtons.Left, 0, x, y, delta)); } else if (Messages.WM_LBUTTONUP == message) { OnMouseUp(new MouseEventArgs(MouseButtons.Left, 0, x, y, delta)); OnMouseClick(new MouseEventArgs(MouseButtons.Left, 0, x, y, delta)); } else if (Messages.WM_MBUTTONDBLCLK == message) { OnMouseDoubleClick(new MouseEventArgs(MouseButtons.Middle, 0, x, y, delta)); } else if (Messages.WM_MBUTTONDOWN == message) { OnMouseDown(new MouseEventArgs(MouseButtons.Middle, 0, x, y, delta)); } else if (Messages.WM_MBUTTONUP == message) { OnMouseUp(new MouseEventArgs(MouseButtons.Middle, 0, x, y, delta)); } else if (Messages.WM_RBUTTONDBLCLK == message) { OnMouseDoubleClick(new MouseEventArgs(MouseButtons.Right, 0, x, y, delta)); } else if (Messages.WM_RBUTTONDOWN == message) { OnMouseDown(new MouseEventArgs(MouseButtons.Right, 0, x, y, delta)); } else if (Messages.WM_RBUTTONUP == message) { OnMouseUp(new MouseEventArgs(MouseButtons.Right, 0, x, y, delta)); } else if (Messages.WM_XBUTTONDBLCLK == message) { OnMouseDoubleClick(new MouseEventArgs(MouseButtons.XButton1, 0, x, y, delta)); } else if (Messages.WM_XBUTTONDOWN == message) { OnMouseDown(new MouseEventArgs(MouseButtons.XButton1, 0, x, y, delta)); } else if (Messages.WM_XBUTTONUP == message) { OnMouseUp(new MouseEventArgs(MouseButtons.XButton1, 0, x, y, delta)); } return(NativeMethods.CallNextHookEx(_handle, code, wParam, lParam)); }