public void ClearHook() { if (_keyboardHookHandle != IntPtr.Zero) { int result = KeyboardHookLowLevel.UnhookWindowsHookEx(_keyboardHookHandle); _keyboardHookHandle = IntPtr.Zero; _keyboardDelegate = null; if (result == 0) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }
public void SetHook() { if (_keyboardHookHandle == IntPtr.Zero) { _modifiers = Keys.None; _keyboardDelegate = KeyboardHookProc; _keyboardHookHandle = KeyboardHookLowLevel.SetWindowsHookEx(WH_KEYBOARD_LL, _keyboardDelegate, Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0); if (_keyboardHookHandle == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } } }