/// <summary> /// 禁用键盘钩子处理 /// </summary> /// <returns>如果禁用成功,则返回True</returns> public static bool Disable() { if (Enabled == true) { try { Hooks.UnhookWindowsHookEx(hHook); Enabled = false; return(true); } catch (Exception ex) { Enabled = true; LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(KeyboardHook)); return(false); } } else { return(false); } }
/// <summary> /// 启动键盘钩子处理 /// </summary> /// <returns>如无异常返回True</returns> public static bool Enable() { if (Enabled == false) { try { using (Process curProcess = Process.GetCurrentProcess()) { using (ProcessModule curModule = curProcess.MainModule) { hHook = Hooks.SetWindowsHookEx((int)HookType.WH_KEYBOARD_LL, hookproc, Hooks.GetModuleHandle(curModule.ModuleName), 0); } } Enabled = true; return(true); } catch (Exception ex) { Enabled = false; LogHelper.WriteLog(LogLevel.LOG_LEVEL_CRIT, ex, typeof(KeyboardHook)); return(false); } } else { return(false); } }
private static IntPtr Filter(int nCode, IntPtr wParam, IntPtr lParam) { bool result = true; if (nCode >= 0) { if (wParam == (IntPtr)Const.WM_KEYDOWN || wParam == (IntPtr)Const.WM_SYSKEYDOWN) { int vkCode = Marshal.ReadInt32(lParam); if ((Keys)vkCode == Keys.LControlKey || (Keys)vkCode == Keys.RControlKey) { Control = true; } else if ((Keys)vkCode == Keys.LShiftKey || (Keys)vkCode == Keys.RShiftKey) { Shift = true; } else if ((Keys)vkCode == Keys.RMenu || (Keys)vkCode == Keys.LMenu) { Alt = true; } else if ((Keys)vkCode == Keys.RWin || (Keys)vkCode == Keys.LWin) { Win = true; } else { result = OnKeyDown((Keys)vkCode); } } else if (wParam == (IntPtr)Const.WM_KEYUP || wParam == (IntPtr)Const.WM_SYSKEYUP) { int vkCode = Marshal.ReadInt32(lParam); if ((Keys)vkCode == Keys.LControlKey || (Keys)vkCode == Keys.RControlKey) { Control = false; } else if ((Keys)vkCode == Keys.LShiftKey || (Keys)vkCode == Keys.RShiftKey) { Shift = false; } else if ((Keys)vkCode == Keys.RMenu || (Keys)vkCode == Keys.LMenu) { Alt = false; } else if ((Keys)vkCode == Keys.RWin || (Keys)vkCode == Keys.LWin) { Win = false; } else { result = OnKeyUp((Keys)vkCode); } } } return(result ? Hooks.CallNextHookEx(hHook, nCode, wParam, lParam) : new IntPtr(1)); }
private static IntPtr Filter(int nCode, IntPtr wParam, IntPtr lParam) { bool result = true; if (nCode >= 0) { Hooks.MouseHookStruct info = (Hooks.MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(Hooks.MouseHookStruct)); switch ((int)wParam) { case Const.WM_LBUTTONDOWN: result = OnMouseDown(MouseButtons.Left); break; case Const.WM_LBUTTONUP: result = OnMouseUp(MouseButtons.Left); break; case Const.WM_RBUTTONDOWN: result = OnMouseDown(MouseButtons.Right); break; case Const.WM_RBUTTONUP: result = OnMouseUp(MouseButtons.Right); break; case Const.WM_MBUTTONDOWN: result = OnMouseDown(MouseButtons.Middle); break; case Const.WM_MBUTTONUP: result = OnMouseUp(MouseButtons.Middle); break; case Const.WM_XBUTTONDOWN: if (info.Data >> 16 == Const.XBUTTON1) { result = OnMouseDown(MouseButtons.XButton1); } else if (info.Data >> 16 == Const.XBUTTON2) { result = OnMouseDown(MouseButtons.XButton2); } break; case Const.WM_XBUTTONUP: if (info.Data >> 16 == Const.XBUTTON1) { result = OnMouseUp(MouseButtons.XButton1); } else if (info.Data >> 16 == Const.XBUTTON2) { result = OnMouseUp(MouseButtons.XButton2); } break; case Const.WM_MOUSEMOVE: result = OnMouseMove(new Point(info.Point.X, info.Point.Y)); break; case Const.WM_MOUSEWHEEL: result = OnMouseWheel((info.Data >> 16) & 0xffff); break; } } return(result ? Hooks.CallNextHookEx(hHook, nCode, wParam, lParam) : new IntPtr(1)); }