public void Stop() { if (this._isStarted) { GlobalHook.UnhookWindowsHookEx(this._handleToHook); this._isStarted = false; } }
protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam) { if (nCode > -1 && (this.MouseDown != null || this.MouseUp != null || this.MouseMove != null)) { GlobalHook.MouseLLHookStruct mouseLLHookStruct = (GlobalHook.MouseLLHookStruct)Marshal.PtrToStructure(lParam, typeof(GlobalHook.MouseLLHookStruct)); MouseButtons button = this.GetButton(wParam); MouseHook.MouseEventType mouseEventType = this.GetEventType(wParam); MouseEventArgs e = new MouseEventArgs(button, (mouseEventType == MouseHook.MouseEventType.DoubleClick) ? 2 : 1, mouseLLHookStruct.pt.x, mouseLLHookStruct.pt.y, (int)((mouseEventType == MouseHook.MouseEventType.MouseWheel) ? ((short)(mouseLLHookStruct.mouseData >> 16 & 65535)) : 0)); if (button == MouseButtons.Right && mouseLLHookStruct.flags != 0) { mouseEventType = MouseHook.MouseEventType.None; } switch (mouseEventType) { case MouseHook.MouseEventType.MouseDown: if (this.MouseDown != null) { this.MouseDown(this, e); } break; case MouseHook.MouseEventType.MouseUp: if (this.Click != null) { this.Click(this, new EventArgs()); } if (this.MouseUp != null) { this.MouseUp(this, e); } break; case MouseHook.MouseEventType.DoubleClick: if (this.DoubleClick != null) { this.DoubleClick(this, new EventArgs()); } break; case MouseHook.MouseEventType.MouseWheel: if (this.MouseWheel != null) { this.MouseWheel(this, e); } break; case MouseHook.MouseEventType.MouseMove: if (this.MouseMove != null) { this.MouseMove(this, e); } break; } } return(GlobalHook.CallNextHookEx(this._handleToHook, nCode, wParam, lParam)); }
public void Start() { if (!this._isStarted && this._hookType != 0) { this._hookCallback = new GlobalHook.HookProc(this.HookCallbackProcedure); this._handleToHook = GlobalHook.SetWindowsHookEx(this._hookType, this._hookCallback, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (this._handleToHook != 0) { this._isStarted = true; } } }
protected override int HookCallbackProcedure(int nCode, int wParam, IntPtr lParam) { bool flag = false; if (nCode > -1 && (this.KeyDown != null || this.KeyUp != null || this.KeyPress != null)) { GlobalHook.KeyboardHookStruct keyboardHookStruct = (GlobalHook.KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(GlobalHook.KeyboardHookStruct)); bool flag2 = (GlobalHook.GetKeyState(162) & 128) != 0 || (GlobalHook.GetKeyState(3) & 128) != 0; bool flag3 = (GlobalHook.GetKeyState(160) & 128) != 0 || (GlobalHook.GetKeyState(161) & 128) != 0; bool flag4 = (GlobalHook.GetKeyState(164) & 128) != 0 || (GlobalHook.GetKeyState(165) & 128) != 0; bool flag5 = GlobalHook.GetKeyState(20) != 0; KeyEventArgs keyEventArgs = new KeyEventArgs((Keys)(keyboardHookStruct.vkCode | (flag2 ? 131072 : 0) | (flag3 ? 65536 : 0) | (flag4 ? 262144 : 0))); switch (wParam) { case 256: case 260: if (this.KeyDown != null) { this.KeyDown(this, keyEventArgs); flag = (flag || keyEventArgs.Handled); } break; case 257: case 261: if (this.KeyUp != null) { this.KeyUp(this, keyEventArgs); flag = (flag || keyEventArgs.Handled); } break; } if (wParam == 256 && !flag && !keyEventArgs.SuppressKeyPress && this.KeyPress != null) { byte[] array = new byte[256]; byte[] array2 = new byte[2]; GlobalHook.GetKeyboardState(array); if (GlobalHook.ToAscii(keyboardHookStruct.vkCode, keyboardHookStruct.scanCode, array, array2, keyboardHookStruct.flags) == 1) { char c = (char)array2[0]; if ((flag5 ^ flag3) && char.IsLetter(c)) { c = char.ToUpper(c); } KeyPressEventArgs e = new KeyPressEventArgs(c); this.KeyPress(this, e); flag = (flag || keyEventArgs.Handled); } } } int result; if (flag) { result = 1; } else { result = GlobalHook.CallNextHookEx(this._handleToHook, nCode, wParam, lParam); } return(result); }