private void MouseEventHandler(MouseHookEventArgs args) { if (args.Button == key) { state = args.State == MouseButtonState.Pressed ? 1 : 0; } }
public void StartHook() { if (hookPtr != IntPtr.Zero) { return; } hook = (nCode, wParam, lParam) => { if (nCode >= 0) { var args = MouseHookEventArgs.Create((MouseMessage)wParam, lParam); if (args != null) { state[args.Button] = args.Pressed; MouseEvent?.Invoke(args); } } return(NativeMethods.CallNextHookEx(hookPtr, nCode, wParam, lParam)); }; hookPtr = NativeMethods.SetWindowsHookEx(HookType.WH_MOUSE_LL, hook, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (hookPtr == IntPtr.Zero) { throw new Win32Exception("Unable to set MouseHook"); } logger.Info("Mouse Windows API hook is set up"); }
public void StartHook() { hook = (nCode, wParam, lParam) => { if (nCode >= 0) { ThreadPool.QueueUserWorkItem((state) => { var args = MouseHookEventArgs.Create((MouseMessage)wParam, lParam); if (args != null) { MouseEvent?.Invoke(args); } }); } return(NativeMethods.CallNextHookEx(hookPtr, nCode, wParam, lParam)); }; hookPtr = NativeMethods.SetWindowsHookEx(HookType.WH_MOUSE_LL, hook, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (hookPtr == IntPtr.Zero) { throw new Win32Exception("Unable to set MouseHook"); } }