/// <summary> /// This is the callback method that is called whenever a low level mouse event is triggered. /// We use it to call our individual custom events. /// </summary> private IntPtr MouseHookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { var lParamStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT)); var e = new MouseHookEventArgs(lParamStruct); switch ((MouseMessages)wParam) { case MouseMessages.WmMouseMove: TriggerMouseEvent(e, MouseEventNames.MouseMove, OnMove); break; case MouseMessages.WmLButtonDown: TriggerMouseEvent(e, MouseEventNames.LeftButtonDown, OnLeftButtonDown); break; case MouseMessages.WmLButtonUp: TriggerMouseEvent(e, MouseEventNames.LeftButtonUp, OnLeftButtonUp); break; case MouseMessages.WmRButtonDown: TriggerMouseEvent(e, MouseEventNames.RightButtonDown, OnRightButtonDown); break; case MouseMessages.WmRButtonUp: TriggerMouseEvent(e, MouseEventNames.RightButtonUp, OnRightButtonUp); break; case MouseMessages.WmMButtonDown: TriggerMouseEvent(e, MouseEventNames.MiddleButtonDown, OnMiddleButtonDown); break; case MouseMessages.WmMButtonUp: TriggerMouseEvent(e, MouseEventNames.MouseMove, OnMove); e.MouseEventName = MouseEventNames.MiddleButtonUp; OnMiddleButtonUp(e); break; case MouseMessages.WmMouseWheel: TriggerMouseEvent(e, MouseEventNames.MouseMove, OnMove); e.MouseEventName = MouseEventNames.MouseWheel; OnWheel(e); break; } } return((IntPtr)User32.CallNextHookEx(_hookId, nCode, wParam, lParam)); }
private void OnMouseEvent(MouseHookEventArgs e) { MouseEvent?.Invoke(this, e); }
private void OnWheel(MouseHookEventArgs e) { Wheel?.Invoke(this, e); OnMouseEvent(e); }
private void OnMiddleButtonUp(MouseHookEventArgs e) { MiddleButtonUp?.Invoke(this, e); OnMouseEvent(e); }
private void OnRightButtonUp(MouseHookEventArgs e) { RightButtonUp?.Invoke(this, e); OnMouseEvent(e); }
private void OnLeftButtonDown(MouseHookEventArgs e) { LeftButtonDown?.Invoke(this, e); OnMouseEvent(e); }
private static void TriggerMouseEvent(MouseHookEventArgs e, MouseEventNames name, Action <MouseHookEventArgs> method) { e.MouseEventName = name; method(e); }