public KeyboardListener() { //The last hook will be garbage collected if we assign it again if (_HookID == IntPtr.Zero) { _HookID = KeyInterceptor.Hook(HookCallback); } }
private IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam) { try { return(InnerHookCallback(nCode, wParam, lParam)); } catch (Exception) { //Ignore this } return(KeyInterceptor.CallNextHookEx(_HookID, nCode, wParam, lParam)); }
private IntPtr InnerHookCallback(int nCode, IntPtr wParam, IntPtr lParam) { if (nCode >= 0) { if (wParam == (IntPtr)KeyInterceptor.KeyDown) { KeyDown(this, new RawKeyEventArgs(Marshal.ReadInt32(lParam), false)); } else if (wParam == (IntPtr)KeyInterceptor.KeyUp) { KeyUp(this, new RawKeyEventArgs(Marshal.ReadInt32(lParam), false)); } } return(KeyInterceptor.CallNextHookEx(_HookID, nCode, wParam, lParam)); }
public void Dispose() { KeyInterceptor.UnhookWindowsHookEx(_HookID); }