private bool OnGlobalKeyDown(Key keyCode) { GlobalKeyEventArgs args = new GlobalKeyEventArgs(keyCode); GlobalKeyDown?.Invoke(this, args); return(args.Handled); }
/// <summary> /// 註冊Windows Hook時用到的委派方法,當全域事件發生時會執行這個方法,並提供全域事件資料。 /// </summary> private static int HookProc(int nCode, IntPtr wParam, IntPtr lParam) { KeyEventArgs e = null; int wParam_Int32 = wParam.ToInt32(); if (nCode >= 0) { Win32Native.Structures.KEYBOARDLLHookStruct keyboardHookStruct = (Win32Native.Structures.KEYBOARDLLHookStruct)Marshal.PtrToStructure(lParam, typeof(Win32Native.Structures.KEYBOARDLLHookStruct)); if (GlobalKeyDown != null && (wParam_Int32 == Win32Native.NativeContansts.WM_KEYDOWN || wParam_Int32 == Win32Native.NativeContansts.WM_SYSKEYDOWN)) { e = new KeyEventArgs(keyboardHookStruct.VirtualKeyCode); GlobalKeyDown.Invoke(null, e); } else if (GlobalKeyUp != null && (wParam_Int32 == Win32Native.NativeContansts.WM_KEYUP || wParam_Int32 == Win32Native.NativeContansts.WM_SYSKEYUP)) { e = new KeyEventArgs(keyboardHookStruct.VirtualKeyCode); GlobalKeyUp.Invoke(null, e); } } if (Monopolize || (e != null && e.Handled)) { return(-1); } return(Win32Native.Methods.CallNextHookEx(m_HookHandle, nCode, wParam, lParam)); }
/// <summary> /// A callback function which will be called every time a keyboard activity is detected. /// </summary> /// <param name="nCode">Specifies whether the hook procedure must process the message.</param> /// <param name="wParam">Specifies whether the message was sent by the current thread.</param> /// <param name="lParam">A pointer to a CWPSTRUCT structure that contains details about the message.</param> /// <returns></returns> private int KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam) { // Indicates if any underlying events handled the action. bool handled = false; if (nCode >= 0) { // Marshal the data from callback. KBDLLHOOKSTRUCT keyStruct = (KBDLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(KBDLLHOOKSTRUCT)); // Key was pressed down. if (GlobalKeyDown != null && (wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN)) { GlobalKeyEventHandlerArgs keyEventArgs = new GlobalKeyEventHandlerArgs( keyStruct.vkCode, keyStruct.scanCode, keyStruct.flags, keyStruct.time, keyStruct.dwExtraInfo); GlobalKeyDown.Invoke(null, keyEventArgs); handled = keyEventArgs.Handled; } // Key was released. if (GlobalKeyUp != null && (wParam == WM_KEYUP || wParam == WM_SYSKEYUP)) { GlobalKeyEventHandlerArgs keyEventArgs = new GlobalKeyEventHandlerArgs( keyStruct.vkCode, keyStruct.scanCode, keyStruct.flags, keyStruct.time, keyStruct.dwExtraInfo); GlobalKeyUp.Invoke(null, keyEventArgs); handled = handled || keyEventArgs.Handled; } } // Exit method here without calling next hook if is event was handled. if (handled) { return(-1); } // Call next hook. return(CallNextHookEx(windowsKeyboardHookHandle, nCode, wParam, lParam)); }
/// <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)); }