Ejemplo n.º 1
0
        public int KeyboardHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //     Debug.Print("KeyboardHook called");
                var keyboardData = (HookNativeDefinitions.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(HookNativeDefinitions.KeyboardHookStruct));
                unchecked
                {
                    // wParam is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN, or WM_SYSKEYUP
                    int wInt = wParam.ToInt32();

                    var key = KeyInterop.KeyFromVirtualKey((int)keyboardData.vkCode);
                    if (wInt == WM.KEYDOWN || wInt == WM.SYSKEYDOWN && OnKeyDown != null)
                    {
                        // OnKeyDown?.Invoke(key);
                        messageQueue.Add(HookMessage.KeyDown(key));
                    }
                    else if (wInt == WM.KEYUP || wInt == WM.SYSKEYUP && OnKeyUp != null)
                    {
                        // OnKeyUp?.Invoke(key);
                        messageQueue.Add(HookMessage.KeyUp(key));
                    }
                }
            }

            return(HookNativeDefinitions.CallNextHookEx(MouseHookHandle, nCode, wParam, lParam));
        }
Ejemplo n.º 2
0
        public int MouseHook(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0)
            {
                //    Debug.Print("MouseHook called");
                unchecked
                {
                    int wParami = wParam.ToInt32();

                    //Marshal the data from the callback.
                    var mouseData   = (HookNativeDefinitions.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(HookNativeDefinitions.MouseHookStruct));
                    var mouseButton = WM.GetMouseButton(wParam);
                    int wheelDelta  = 0;
                    if (mouseButton.button == MouseButtons.None && wParami == WM.MOUSEWHEEL)
                    {
                        wheelDelta = WM.GetWheelDDelta(mouseData.mouseData);
                    }

                    if (wParami == WM.MOUSEMOVE)
                    {
                        if (OnMouseMove != null)
                        {
                            // OnMouseMove(mouseData.pt.x, mouseData.pt.y);
                        }
                        else
                        {
                            // mouse move is disabled ignore event
                        }
                    }
                    else if (wParami == WM.MOUSEWHEEL && OnMouseWheel != null)
                    {
                        //OnMouseWheel?.Invoke(wheelDelta, mouseData.pt.x, mouseData.pt.y);
                    }
                    else if (OnMouseButton != null)
                    {
                        //OnMouseButton?.Invoke(mouseButton, mouseData.pt.x, mouseData.pt.y);
                        messageQueue.Add(HookMessage.MouseDown(mouseButton.button, mouseData.pt.x, mouseData.pt.y));
                    }
                }
            }

            return(HookNativeDefinitions.CallNextHookEx(MouseHookHandle, nCode, wParam, lParam));
        }