internal static extern bool GetWindowOrgEx(IntPtr hdc, out PointW lpPoint);
internal static extern bool ClientToScreen(IntPtr hWnd, ref PointW lpPoint);
internal static extern bool GetCurrentPositionEx(IntPtr hdc, out PointW lpPoint);
/// <summary> /// A callback function which will be called every time a mouse activity detected. /// https://docs.microsoft.com/en-us/windows/win32/winmsg/lowlevelmouseproc /// </summary> /// <param name="code"> /// Specifies whether the hook procedure must process the message. /// If code is HC_ACTION, the hook procedure must process the message. /// If code is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the value returned by CallNextHookEx. /// </param> /// <param name="type"> /// Same as wParam. Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="structure"> /// Same as lParam. Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private IntPtr MouseHookProc(int code, uint type, IntPtr structure) { //If it's not Ok or no one listens to this event, call next hook. if (code < 0 || OnMouseActivity == null) { return(User32.CallNextHookEx(_mouseHookHandle, code, type, structure)); } //Marshall the data from callback. var mouse = (MouseHook)Marshal.PtrToStructure(structure, typeof(MouseHook)); var data = new WordLevel.WordUnion { Number = mouse.MouseData }; #region Mouse actions switch ((NativeMouseEvents)type) { case NativeMouseEvents.MouseMove: { if (!_isDragging && _leftButton == MouseButtonState.Pressed) { var isXDragging = Math.Abs(mouse.Point.X - _dragStartPoint.X) > SystemParameters.MinimumHorizontalDragDistance; var isYDragging = Math.Abs(mouse.Point.Y - _dragStartPoint.Y) > SystemParameters.MinimumVerticalDragDistance; _isDragging = isXDragging || isYDragging; if (_isDragging) { OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MouseDragStart, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } } OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MouseMove, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideLeftButtonDown: case NativeMouseEvents.LeftButtonDown: { DetectDoubleClick(NativeMouseEvents.LeftButtonDoubleClick, mouse.Point); _leftButton = MouseButtonState.Pressed; _dragStartPoint = mouse.Point; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.LeftButtonDown, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideLeftButtonUp: case NativeMouseEvents.LeftButtonUp: { //End drag. if (_isDragging) { OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MouseDragEnd, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); _isDragging = false; } _leftButton = MouseButtonState.Released; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.LeftButtonUp, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideLeftButtonDoubleClick: case NativeMouseEvents.LeftButtonDoubleClick: { _leftButton = MouseButtonState.Pressed; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.LeftButtonDoubleClick, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); _leftButton = MouseButtonState.Released; break; } case NativeMouseEvents.OutsideRightButtonDown: case NativeMouseEvents.RightButtonDown: { DetectDoubleClick(NativeMouseEvents.RightButtonDoubleClick, mouse.Point); _rightButton = MouseButtonState.Pressed; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.RightButtonDown, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideRightButtonUp: case NativeMouseEvents.RightButtonUp: { _rightButton = MouseButtonState.Released; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.RightButtonUp, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideRightButtonDoubleClick: case NativeMouseEvents.RightButtonDoubleClick: { _rightButton = MouseButtonState.Pressed; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.RightButtonDoubleClick, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); _rightButton = MouseButtonState.Released; break; } case NativeMouseEvents.OutsideMiddleButtonDown: case NativeMouseEvents.MiddleButtonDown: { DetectDoubleClick(NativeMouseEvents.MiddleButtonDoubleClick, mouse.Point); _middleButton = MouseButtonState.Pressed; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MiddleButtonDown, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideMiddleButtonUp: case NativeMouseEvents.MiddleButtonUp: { _middleButton = MouseButtonState.Released; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MiddleButtonUp, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideMiddleButtonDoubleClick: case NativeMouseEvents.MiddleButtonDoubleClick: { _middleButton = MouseButtonState.Pressed; OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MiddleButtonDoubleClick, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); _middleButton = MouseButtonState.Released; break; } case NativeMouseEvents.MouseWheel: { OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MouseWheel, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button, data.High)); break; } case NativeMouseEvents.MouseWheelHorizontal: { OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.MouseWheelHorizontal, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button, data.High)); break; } case NativeMouseEvents.OutsideExtraButtonDown: case NativeMouseEvents.ExtraButtonDown: { DetectDoubleClick(NativeMouseEvents.ExtraButtonDoubleClick, mouse.Point); if (data.High == MouseFirstExtraButton) { _extraButton = MouseButtonState.Pressed; } else { _extra2Button = MouseButtonState.Pressed; } OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.ExtraButtonDown, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } case NativeMouseEvents.OutsideExtraButtonDoubleClick: case NativeMouseEvents.ExtraButtonDoubleClick: { if (data.High == MouseFirstExtraButton) { _extraButton = MouseButtonState.Pressed; } else { _extra2Button = MouseButtonState.Pressed; } OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.ExtraButtonDoubleClick, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); if (data.High == MouseFirstExtraButton) { _extraButton = MouseButtonState.Released; } else { _extra2Button = MouseButtonState.Released; } break; } case NativeMouseEvents.OutsideExtraButtonUp: case NativeMouseEvents.ExtraButtonUp: { if (data.High == MouseFirstExtraButton) { _extraButton = MouseButtonState.Released; } else { _extra2Button = MouseButtonState.Released; } OnMouseActivity?.Invoke(this, new SimpleMouseGesture(NativeMouseEvents.ExtraButtonUp, mouse.Point.X, mouse.Point.Y, _leftButton, _rightButton, _middleButton, _extraButton, _extra2Button)); break; } //default: I can't return now, it will break the click detector. //return CallNextHookEx(hMouseHook, nCode, wParam, lParam); //HU3HU3 - A little funny momment: I just frooze my cursor by returning 1 instead of calling the next hook. - Nicke //Congrats to myself. ;D //05:24 AM 01/02/2014 (day-month-year) } #endregion //Call the next hook. return(User32.CallNextHookEx(_mouseHookHandle, code, type, structure)); }