Beispiel #1
0
        public void Dispose()
        {
            if (MouseHookHandle != 0)
            {
                Debug.Print("Unhook mouse");
                HookNativeDefinitions.UnhookWindowsHookEx(MouseHookHandle);
                MouseHookHandle = 0;
            }

            if (KeyboardHookHandle != 0)
            {
                Debug.Print("Unhook Keyboard");
                HookNativeDefinitions.UnhookWindowsHookEx(KeyboardHookHandle);
                KeyboardHookHandle = 0;
            }
        }
Beispiel #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 == MouseButton.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);
                    }
                }
            }

            return(HookNativeDefinitions.CallNextHookEx(MouseHookHandle, nCode, wParam, lParam));
        }
Beispiel #3
0
 private void HookMouse(bool bHook)
 {
     if (MouseHookHandle == 0 && bHook)
     {
         using (var mainMod = Process.GetCurrentProcess().MainModule)
             MouseHookHandle = HookNativeDefinitions.SetWindowsHookEx(HookNativeDefinitions.WH_MOUSE_LL, MouseHookGCRootedDelegate, HookNativeDefinitions.GetModuleHandle(mainMod.ModuleName), 0);
         //If the SetWindowsHookEx function fails.
         if (MouseHookHandle == 0)
         {
             System.Windows.MessageBox.Show("SetWindowsHookEx Failed " + new Win32Exception(Marshal.GetLastWin32Error()));
             return;
         }
     }
     else if (bHook == false)
     {
         Debug.Print("Unhook mouse");
         HookNativeDefinitions.UnhookWindowsHookEx(MouseHookHandle);
         MouseHookHandle = 0;
     }
 }
Beispiel #4
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);
                    }
                    else
                    {
                    }
                }
            }

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