예제 #1
0
파일: Program.cs 프로젝트: wyrover/HookLib
 static void GlobalKeyDown(object sender, GlobalKeyEventHandlerArgs args)
 {
     if (args.ScanCode == 30)
     {
         args.Handled = true;
     }
     System.Console.WriteLine(args.ScanCode);
 }
예제 #2
0
 private void hook_KeyUp(object sender, GlobalKeyEventHandlerArgs e)
 {
     if (IsRecording)
     {
         AddDelayEvent();
         CurrentMacro.AddEvent(new MacroKeyUpEvent(e.VirtualKeyCode));
     }
 }
예제 #3
0
        static void GlobalKeyPressed(object sender, GlobalKeyEventHandlerArgs args)
        {
            // ------------------Print keyboard activity into LogFile------------------

            log.WriteLine(args.Time + ":KeyPressed:" + args.Character);
            log.Flush();

            // ------------------Print keyboard activity into LogFile------------------
        }
        /// <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));
        }
예제 #5
0
        /// <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));
        }
예제 #6
0
        /// <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);
        }