private void Initialize() { this.callback = (int nCode, IntPtr wParam, IntPtr lParam) => { HookStruct info = (HookStruct)Marshal.PtrToStructure(lParam, typeof(HookStruct)); bool flag = nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN; if (flag) { int vkCode = Marshal.ReadInt32(lParam); GlobalKeyPressed?.Invoke(KeyInterop.KeyFromVirtualKey(vkCode)); } if (LockAllKeys && info.flags != LLKHF_INJECTED) { return((IntPtr)1); } else { return(CallNextHookEx(_hookID, nCode, wParam, lParam)); } }; _hookID = SetHook(this.callback); }
/// <summary> /// A callback function which will be called every Time a keyboard activity detected. /// </summary> /// <param name="nCode"> /// [in] Specifies whether the hook procedure must process the message. /// If nCode is HC_ACTION, the hook procedure must process the message. /// If nCode is less than zero, the hook procedure must pass the message to the /// CallNextHookEx function without further processing and must return the /// value returned by CallNextHookEx.l /// </param> /// <param name="wParam"> /// [in] Specifies whether the message was sent by the current thread. /// If the message was sent by the current thread, it is nonzero; otherwise, it is zero. /// </param> /// <param name="lParam"> /// [in] Pointer to a CWPSTRUCT structure that contains details about the message. /// </param> /// <returns> /// If nCode is less than zero, the hook procedure must return the value returned by CallNextHookEx. /// If nCode is greater than or equal to zero, it is highly recommended that you call CallNextHookEx /// and return the value it returns; otherwise, other applications that have installed WH_CALLWNDPROC /// hooks will not receive hook notifications and may behave incorrectly as a result. If the hook /// procedure does not call CallNextHookEx, the return value should be zero. /// </returns> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { //indicates if any of underlaing events set e.Handled flag var handled = false; if (nCode >= 0) { //read structure KeyboardHookStruct at lParam var keyStruct = (KeyboardHookStruct)Marshal.PtrToStructure(lParam, typeof(KeyboardHookStruct)); //raise KeyDown if (GlobalKeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { var keyEventArgs = new GlobalKeyEventHandlerArgs( keyStruct.VirtualKeyCode, keyStruct.ScanCode, keyStruct.Flags, keyStruct.Time, keyStruct.ExtraInfo, null); GlobalKeyDown.Invoke(null, keyEventArgs); handled = keyEventArgs.Handled; } // raise KeyPress if (GlobalKeyPressed != null && wParam == WM_KEYDOWN) { var isDownShift = ((GetKeyState(VK_SHIFT) & 0x80) == 0x80 ? true : false); var isDownCapslock = (GetKeyState(VK_CAPITAL) != 0 ? true : false); var keyState = new byte[256]; GetKeyboardState(keyState); var inBuffer = new byte[2]; if (ToAscii(keyStruct.VirtualKeyCode, keyStruct.ScanCode, keyState, inBuffer, keyStruct.Flags) == 1) { var key = (char)inBuffer[0]; if ((isDownCapslock ^ isDownShift) && Char.IsLetter(key)) { key = Char.ToUpper(key); } var keyEventArgs = new GlobalKeyEventHandlerArgs( keyStruct.VirtualKeyCode, keyStruct.ScanCode, keyStruct.Flags, keyStruct.Time, keyStruct.ExtraInfo, key); GlobalKeyPressed.Invoke(null, keyEventArgs); handled = handled || keyEventArgs.Handled; } } // raise KeyUp if (GlobalKeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { var keyEventArgs = new GlobalKeyEventHandlerArgs( keyStruct.VirtualKeyCode, keyStruct.ScanCode, keyStruct.Flags, keyStruct.Time, keyStruct.ExtraInfo, null); GlobalKeyUp.Invoke(null, keyEventArgs); handled = handled || keyEventArgs.Handled; } } //if event handled in application do not handoff to other listeners if (handled) { return(-1); } //forward to other application return(CallNextHookEx(_windowsKeyboardHookHandle, nCode, wParam, lParam)); }