private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam) { // Parse system messages if (nCode >= 0) { if (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam) { LeftButtonDown?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_LBUTTONUP == (MouseMessages)wParam) { LeftButtonUp?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam) { RightButtonDown?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_RBUTTONUP == (MouseMessages)wParam) { RightButtonUp?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_MOUSEMOVE == (MouseMessages)wParam) { MouseMove?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_MOUSEWHEEL == (MouseMessages)wParam) { MouseWheel?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_LBUTTONDBLCLK == (MouseMessages)wParam) { DoubleClick?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_MBUTTONDOWN == (MouseMessages)wParam) { MiddleButtonDown?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } if (MouseMessages.WM_MBUTTONUP == (MouseMessages)wParam) { MiddleButtonUp?.Invoke( (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT))); } } return(CallNextHookEx(_hookId, nCode, wParam, lParam)); }
public void Update() { var mouseState = _mouse.GetState(); if (mouseState.LeftButton != _previousMouseState.LeftButton) { if (mouseState.LeftButton == ButtonState.Pressed) { LeftButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState)); } else { LeftButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState)); } } if (mouseState.MiddleButton != _previousMouseState.MiddleButton) { if (mouseState.MiddleButton == ButtonState.Pressed) { MiddleButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState)); } else { MiddleButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState)); } } if (mouseState.RightButton != _previousMouseState.RightButton) { if (mouseState.RightButton == ButtonState.Pressed) { RightButtonDown?.Invoke(this, new MouseEventArgs(ref mouseState)); } else { RightButtonUp?.Invoke(this, new MouseEventArgs(ref mouseState)); } } if (mouseState.Position != _previousMouseState.Position) { MouseMove?.Invoke(this, new MouseMoveEventArgs(mouseState, _previousMouseState.Position)); } if (mouseState.ScrollWheelValue != _previousMouseState.ScrollWheelValue) { MouseWheel?.Invoke(this, new MouseWheelEventArgs(mouseState, mouseState.ScrollWheelValue - _previousMouseState.ScrollWheelValue)); } _previousMouseState = mouseState; }
/// <summary> /// Callback function /// </summary> private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam) { // parse system messages if (nCode < 0) { return(NativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam)); } // ReSharper disable once SwitchStatementMissingSomeCases switch ((MouseFlags)wParam) { case MouseFlags.LeftDown: LeftButtonDown?.Invoke((MouseInput)Marshal.PtrToStructure(lParam, typeof(MouseInput))); break; case MouseFlags.LeftUp: LeftButtonUp?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.RightDown: RightButtonDown?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.RightUp: RightButtonUp?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.Move: MouseMove?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.Wheel: MouseWheel?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.DoubleLeftClick: DoubleClick?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.MiddleDown: MiddleButtonDown?.Invoke(MouseInputToStructure(lParam)); break; case MouseFlags.MiddleUp: MiddleButtonUp?.Invoke(MouseInputToStructure(lParam)); break; } return(NativeMethods.CallNextHookEx(HookId, nCode, wParam, lParam)); }
/// <summary> /// Callback function /// </summary> protected override IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam) { // parse system messages if (nCode >= 0) { switch ((WM_MESSAGE)wParam) { case WM_MESSAGE.WM_LBUTTONDOWN: LeftButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_LBUTTONUP: LeftButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_RBUTTONDOWN: RightButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_RBUTTONUP: RightButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_MOUSEMOVE: MouseMove?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_MOUSEWHEEL: MouseWheel?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_LBUTTONDBLCLK: DoubleClick?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_MBUTTONDOWN: MiddleButtonDown?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; case WM_MESSAGE.WM_MBUTTONUP: MiddleButtonUp?.Invoke((MOUSEINPUT)Marshal.PtrToStructure(lParam, typeof(MOUSEINPUT))); break; } } return(CallNextHookEx(hookID, nCode, wParam, lParam)); }
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> /// Callback function /// </summary> private IntPtr HookFunc(int nCode, IntPtr wParam, IntPtr lParam) { // parse system messages if (nCode >= 0) { var data = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var message = (MouseMessages)wParam; var isDown = false; var isUp = false; SimpleMouseButtons button; switch (message) { case MouseMessages.WM_MOUSEMOVE: break; case MouseMessages.WM_LBUTTONDOWN: isDown = true; break; case MouseMessages.WM_RBUTTONDOWN: isDown = true; break; case MouseMessages.WM_MBUTTONDOWN: isDown = true; break; case MouseMessages.WM_LBUTTONUP: isUp = true; break; case MouseMessages.WM_RBUTTONUP: isUp = true; break; case MouseMessages.WM_MBUTTONUP: isUp = true; break; case MouseMessages.WM_MOUSEWHEEL: break; case MouseMessages.WM_MOUSEHWHEEL: break; case MouseMessages.WM_LBUTTONDBLCLK: break; case MouseMessages.WM_RBUTTONDBLCLK: break; case MouseMessages.WM_MBUTTONDBLCLK: break; case MouseMessages.WM_XBUTTONDOWN: isDown = true; break; case MouseMessages.WM_XBUTTONUP: isUp = true; break; case MouseMessages.WM_XBUTTONDBLCLK: break; default: break; } if (MouseMessages.WM_LBUTTONDOWN == message) { LeftButtonDown?.Invoke(data); } if (MouseMessages.WM_LBUTTONUP == message) { LeftButtonUp?.Invoke(data); } if (MouseMessages.WM_RBUTTONDOWN == message) { RightButtonDown?.Invoke(data); } if (MouseMessages.WM_RBUTTONUP == message) { RightButtonUp?.Invoke(data); } if (MouseMessages.WM_MOUSEMOVE == message) { MouseMove?.Invoke(data); } if (MouseMessages.WM_MOUSEWHEEL == message) { MouseWheel?.Invoke(data); } if (MouseMessages.WM_LBUTTONDBLCLK == message) { DoubleClick?.Invoke(data); } if (MouseMessages.WM_MBUTTONDOWN == message) { MiddleButtonDown?.Invoke(data); } if (MouseMessages.WM_MBUTTONUP == message) { MiddleButtonUp?.Invoke(data); } //Generic Event, fire with type MouseEvent?.Invoke(data, message); } return(CallNextHookEx(hookID, nCode, wParam, lParam)); }
private void OnMiddleButtonDown(MouseHookEventArgs e) { MiddleButtonDown?.Invoke(this, e); OnMouseEvent(e); }
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); } }