/// <summary> /// 安装钩子 /// </summary> /// <returns></returns> public bool InstallHook(HookType flagsinfo) { this.flags = flagsinfo; IntPtr pInstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().ManifestModule); // 假如没有安装鼠标钩子 if (this.m_pMouseHook == IntPtr.Zero) { this.m_MouseHookProcedure = new HookProc(this.MouseHookProc); //注册鼠标钩子 this.m_pMouseHook = Hocy_Hook.SetWindowsHookEx(WH_Codes.WH_MOUSE_LL, this.m_MouseHookProcedure, pInstance, 0); if (this.m_pMouseHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } // 假如没有安装键盘钩子 if (this.m_pKeyboardHook == IntPtr.Zero) { this.m_KeyboardHookProcedure = new HookProc(this.KeyboardHookProc); //注册键盘钩子 this.m_pKeyboardHook = Hocy_Hook.SetWindowsHookEx(WH_Codes.WH_KEYBOARD_LL, this.m_KeyboardHookProcedure, pInstance, 0); if (this.m_pKeyboardHook == IntPtr.Zero) { this.UnInstallHook(); return(false); } } return(true); }
public Form1() { InitializeComponent(); hook_Main = new Hocy_Hook(); this.hook_Main.OnMouseActivity += new MouseEventHandler(Hook_MainMouseMove); this.hook_Main.OnKeyDown += new KeyEventHandler(Hook_MainKeyDown); this.hook_Main.OnKeyPress += new KeyPressEventHandler(Hook_MainKeyPress); this.hook_Main.OnKeyUp += new KeyEventHandler(Hook_MainKeyUp); }
/// <summary> /// 卸载钩子 /// </summary> /// <returns></returns> public bool UnInstallHook() { bool result = true; if (this.m_pMouseHook != IntPtr.Zero) { result = (Hocy_Hook.UnhookWindowsHookEx(this.m_pMouseHook) && result); this.m_pMouseHook = IntPtr.Zero; } if (this.m_pKeyboardHook != IntPtr.Zero) { result = (Hocy_Hook.UnhookWindowsHookEx(this.m_pKeyboardHook) && result); this.m_pKeyboardHook = IntPtr.Zero; } return(result); }
/// <summary> 键盘钩子处理函数 /// </summary> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { //为 2 ,直接返回,不响应键盘事件 if (flags == HookType.Keyboard || flags == HookType.All) { } else { return(Hocy_Hook.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam)); } bool handled = false; //it was ok and someone listens to events if ((nCode >= 0) && (this.OnKeyDown != null || this.OnKeyUp != null || this.OnKeyPress != null)) { //read structure KeyboardHookStruct at lParam KeyboardHookStruct MyKeyboardHookStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); //raise KeyDown if (this.OnKeyDown != null && (wParam == (int)WM_KEYBOARD.WM_KEYDOWN || wParam == (int)WM_KEYBOARD.WM_SYSKEYDOWN)) { Keys keyData = (Keys)MyKeyboardHookStruct.VKCode; KeyEventArgs e = new KeyEventArgs(keyData); this.OnKeyDown(this, e); handled = handled || e.Handled; } // raise KeyPress if (this.OnKeyPress != null && wParam == (int)WM_KEYBOARD.WM_KEYDOWN) { bool isDownShift, isDownCapslock; try { isDownShift = ((Hocy_Hook.GetKeyStates(VK_SHIFT) & 0x80) == 0x80 ? true : false); isDownCapslock = (Hocy_Hook.GetKeyStates(VK_CAPITAL) != 0 ? true : false); } catch { isDownCapslock = false; isDownShift = false; } byte[] keyState = new byte[256]; Hocy_Hook.GetKeyboardState(keyState); byte[] inBuffer = new byte[2]; if (Hocy_Hook.ToAscii(MyKeyboardHookStruct.VKCode, MyKeyboardHookStruct.ScanCode, keyState, inBuffer, MyKeyboardHookStruct.Flags) == 1) { char key = (char)inBuffer[0]; if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) { key = Char.ToUpper(key); } KeyPressEventArgs e = new KeyPressEventArgs(key); this.OnKeyPress(this, e); handled = handled || e.Handled; } } // raise KeyUp if (this.OnKeyUp != null && (wParam == (int)WM_KEYBOARD.WM_KEYUP || wParam == (int)WM_KEYBOARD.WM_SYSKEYUP)) { Keys keyData = (Keys)MyKeyboardHookStruct.VKCode; KeyEventArgs e = new KeyEventArgs(keyData); this.OnKeyUp(this, e); handled = handled || e.Handled; } } //handled 为true, 则其他程序无法接受到键盘事件 if (handled) { return(1); } else { return(Hocy_Hook.CallNextHookEx(this.m_pKeyboardHook, nCode, wParam, lParam)); } }
/// <summary> 鼠标钩子处理函数 /// </summary> private int MouseHookProc(int nCode, Int32 wParam, IntPtr lParam) { if (flags == HookType.Mouse || flags == HookType.All) { } else { return(Hocy_Hook.CallNextHookEx(this.m_pMouseHook, nCode, wParam, lParam)); } if ((nCode >= 0) && (OnMouseActivity != null)) { //Marshall the data from callback. MouseHookStruct mouseHookStruct = (MouseHookStruct)Marshal.PtrToStructure(lParam, typeof(MouseHookStruct)); //进行数据转换 MouseButtons button = MouseButtons.None; short mouseDelta = 0; switch (wParam) { case (int)WM_MOUSE.WM_LBUTTONDOWN: //case WM_LBUTTONUP: //case WM_LBUTTONDBLCLK: button = MouseButtons.Left; break; case (int)WM_MOUSE.WM_RBUTTONDOWN: //case WM_RBUTTONUP: //case WM_RBUTTONDBLCLK: button = MouseButtons.Right; break; case (int)WM_MOUSE.WM_MOUSEWHEEL: //If the message is WM_MOUSEWHEEL, the high-order word of mouseData member is the wheel delta. //One wheel click is defined as WHEEL_DELTA, which is 120. //(value >> 16) & 0xffff; retrieves the high-order word from the given 32-bit value mouseDelta = (short)((mouseHookStruct.MouseData >> 16) & 0xffff); //TODO: X BUTTONS (I havent them so was unable to test) //If the message is WM_XBUTTONDOWN, WM_XBUTTONUP, WM_XBUTTONDBLCLK, WM_NCXBUTTONDOWN, WM_NCXBUTTONUP, //or WM_NCXBUTTONDBLCLK, the high-order word specifies which X button was pressed or released, //and the low-order word is reserved. This value can be one or more of the following values. //Otherwise, mouseData is not used. break; } //double clicks int clickCount = 0; if (button != MouseButtons.None) { if (wParam == (int)WM_MOUSE.WM_LBUTTONDBLCLK || wParam == (int)WM_MOUSE.WM_RBUTTONDBLCLK) { clickCount = 2; } else { clickCount = 1; } } //触发事件 //generate event MouseEventArgs e = new MouseEventArgs( button, clickCount, mouseHookStruct.Point.X, mouseHookStruct.Point.Y, mouseDelta); //raise it OnMouseActivity(this, e); } //* return(Hocy_Hook.CallNextHookEx(this.m_pMouseHook, nCode, wParam, lParam)); }
/// <summary> 钩子类,实现 键盘和鼠标的钩子 /// </summary> public Hocy_Hook() { Hocy_Hook.GetKeyboardState(this.m_KeyState); }