Esempio n. 1
0
        protected virtual void Dispose(bool disposing)
        {
            if (_disposed)
            {
                return;
            }

            if (_keyboardHandle != IntPtr.Zero)
            {
                WindowsHookHelper.UnhookWindowsHookEx(_keyboardHandle);
            }

            _disposed = true;
        }
Esempio n. 2
0
        private IntPtr KeyboardHookDelegate(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code >= 0)
            {
                if (wParam == (IntPtr)WM_KEYDOWN)
                {
                    var vkCode       = (Keys)Marshal.ReadInt32(lParam);
                    var keyEventArgs = new KeyEventArgs(vkCode);
                    OnKeyDown(keyEventArgs);
                }
                else if (wParam == (IntPtr)WM_KEYUP)
                {
                    var vkCode       = (Keys)Marshal.ReadInt32(lParam);
                    var keyEventArgs = new KeyEventArgs(vkCode);
                    OnKeyUp(keyEventArgs);
                }
            }

            return(WindowsHookHelper.CallNextHookEx(_keyboardHandle, code, wParam, lParam));
        }
Esempio n. 3
0
        private IntPtr KeyboardHookDelegate(int code, IntPtr wParam, IntPtr lParam)
        {
            if (code >= 0)
            {
                var            arg = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
                MouseEventArgs mouseEventArgs;
                switch ((int)wParam)
                {
                case WM_MOUSEMOVE:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.None, 0, arg.pt.x, arg.pt.y, 0);
                    OnMouseMoved(mouseEventArgs);
                    break;

                case WM_MOUSEWHEEL:
                    var delta = 0;
                    if (arg.mouseData > 0)
                    {
                        delta = 1;
                    }
                    else if (arg.mouseData < 0)
                    {
                        delta = -1;
                    }

                    mouseEventArgs = new MouseEventArgs(MouseButtons.None, 0, 0, 0, delta);
                    OnMouseWheel(mouseEventArgs);
                    break;

                case WM_LBUTTONDBLCLK:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Left, 2, arg.pt.x, arg.pt.y, 0);
                    OnMouseDoubleClicked(mouseEventArgs);
                    break;

                case WM_RBUTTONDBLCLK:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Right, 2, arg.pt.x, arg.pt.y, 0);
                    OnMouseDoubleClicked(mouseEventArgs);
                    break;

                case WM_MBUTTONDBLCLK:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Middle, 2, arg.pt.x, arg.pt.y, 0);
                    OnMouseDoubleClicked(mouseEventArgs);
                    break;

                case WM_XBUTTONDBLCLK:
                    mouseEventArgs = new MouseEventArgs(arg.mouseData == 1 ? MouseButtons.XButton1 : MouseButtons.XButton2, 2, arg.pt.x, arg.pt.y, 0);
                    OnMouseDoubleClicked(mouseEventArgs);
                    break;

                case WM_LBUTTONUP:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Left, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseClicked(mouseEventArgs);
                    OnMouseUp(mouseEventArgs);
                    break;

                case WM_RBUTTONUP:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Right, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseClicked(mouseEventArgs);
                    OnMouseUp(mouseEventArgs);
                    break;

                case WM_MBUTTONUP:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Middle, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseClicked(mouseEventArgs);
                    OnMouseUp(mouseEventArgs);
                    break;

                case WM_XBUTTONUP:
                    mouseEventArgs = new MouseEventArgs(arg.mouseData == 1 ? MouseButtons.XButton1 : MouseButtons.XButton2, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseClicked(mouseEventArgs);
                    OnMouseUp(mouseEventArgs);
                    break;

                case WM_LBUTTONDOWN:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Left, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseDown(mouseEventArgs);
                    break;

                case WM_RBUTTONDOWN:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Right, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseDown(mouseEventArgs);
                    break;

                case WM_MBUTTONDOWN:
                    mouseEventArgs = new MouseEventArgs(MouseButtons.Middle, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseDown(mouseEventArgs);
                    break;

                case WM_XBUTTONDOWN:
                    mouseEventArgs = new MouseEventArgs(arg.mouseData == 1 ? MouseButtons.XButton1 : MouseButtons.XButton2, 1, arg.pt.x, arg.pt.y, 0);
                    OnMouseDown(mouseEventArgs);
                    break;
                }
            }

            return(WindowsHookHelper.CallNextHookEx(_mouseHandle, code, wParam, lParam));
        }
Esempio n. 4
0
 public KeyboardGlobalHook()
 {
     _delegate       = KeyboardHookDelegate;
     _keyboardHandle = WindowsHookHelper.SetWindowsHookEx(WH_KEYBOARD_LL, _delegate, IntPtr.Zero, 0);
 }
Esempio n. 5
0
 public MouseGlobalHook()
 {
     _delegate    = KeyboardHookDelegate;
     _mouseHandle = WindowsHookHelper.SetWindowsHookEx(WH_MOUSE_LL, _delegate, IntPtr.Zero, 0);
 }