private IntPtr LowLevelKeyboardProc(int nCode, UIntPtr wParam, IntPtr lParam) { //Console.WriteLine("got event"); if (nCode >= 0) { if (wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYDOWN || wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYUP || wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYDOWN || wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYUP) { hookedKeyboardCallbackAsync.BeginInvoke((InterceptKeys.KeyEvent)wParam.ToUInt32(), Marshal.ReadInt32(lParam), null, null); } } //return (IntPtr) 1; return(InterceptKeys.CallNextHookEx(hookId, nCode, wParam, lParam)); }
public void Hook(bool getNextKeyOnly = false) { // We have to store the HookCallback, so that it is not garbage collected runtime hookedLowLevelKeyboardProc = (InterceptKeys.LowLevelKeyboardProc)LowLevelKeyboardProc; // Set the hook hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc); // Assign the asynchronous callback event if (getNextKeyOnly) { hookedKeyboardCallbackAsync = new KeyboardCallbackAsync(KeyboardListener_KeyboardCallbackAsyncOneKey); } else { hookedKeyboardCallbackAsync = new KeyboardCallbackAsync(KeyboardListener_KeyboardCallbackAsync); } }
public void UnHook() { InterceptKeys.UnhookWindowsHookEx(hookId); }
/// <summary> /// Disposes the hook. /// <remarks>This call is required as it calls the UnhookWindowsHookEx.</remarks> /// </summary> public void Dispose() { InterceptKeys.UnhookWindowsHookEx(hookId); }
void KeyboardListener_KeyboardCallbackAsyncOneKey(InterceptKeys.KeyEvent keyEvent, int vkCode) { switch (keyEvent) { // KeyDown events case InterceptKeys.KeyEvent.WM_KEYDOWN: if (KeyDown != null) KeyDown(this, new RawKeyEventArgs(vkCode, false)); break; case InterceptKeys.KeyEvent.WM_SYSKEYDOWN: if (KeyDown != null) KeyDown(this, new RawKeyEventArgs(vkCode, true)); break; // KeyUp events case InterceptKeys.KeyEvent.WM_KEYUP: if (KeyUp != null) KeyUp(this, new RawKeyEventArgs(vkCode, false)); break; case InterceptKeys.KeyEvent.WM_SYSKEYUP: if (KeyUp != null) KeyUp(this, new RawKeyEventArgs(vkCode, true)); break; default: break; } this.Dispose(); }