Example #1
0
        /// <summary>
        /// Creates global keyboard listener.
        /// </summary>
        public KeyboardListener()
        {
            // Dispatcher thread handling the KeyDown/KeyUp events.
            this.dispatcher = Dispatcher.CurrentDispatcher;

            // We have to store the LowLevelKeyboardProc, so that it is not garbage collected runtime
            hookedLowLevelKeyboardProc = (InterceptKeys.LowLevelKeyboardProc)LowLevelKeyboardProc;

            // Set the hook
            hookId = InterceptKeys.SetHook(hookedLowLevelKeyboardProc);

            // Assign the asynchronous callback event
            hookedKeyboardCallbackAsync = new KeyboardCallbackAsync(KeyboardListener_KeyboardCallbackAsync);
        }
Example #2
0
        private int LowLevelKeyboardProc(int nCode, UIntPtr wParam, ref InterceptKeys.KBDLLHOOKSTRUCT lParam)
        {
            string chars = "";

            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)
                {
                    // Captures the character(s) pressed only on WM_KEYDOWN
                    chars = InterceptKeys.VKCodeToString((uint)lParam.vkCode,
                                                         (wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYDOWN ||
                                                          wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYDOWN));

                    hookedKeyboardCallbackAsync.BeginInvoke((InterceptKeys.KeyEvent)wParam.ToUInt32(), lParam.vkCode, chars, null, null);
                }
            }

            //---My Block
            uint iwParam = wParam.ToUInt32();

            Debug.WriteLine(iwParam);
            if (iwParam == 256)           // key down message
            {
                if (lParam.vkCode == 162) // control key down
                {
                    ctrl_pressed = true;
                }
            }
            if (iwParam == 257)           // key up message
            {
                if (lParam.vkCode == 162) // control key up
                {
                    ctrl_pressed = false;
                }
            }

            bool forbidden = ForbiddenInput(lParam.vkCode, lParam.flags);

            if (ForbiddenInput(lParam.vkCode, lParam.flags))
            {
                return(1);
            }
            //---end

            return(InterceptKeys.CallNextHookEx(hookId, nCode, wParam, ref lParam));
        }
Example #3
0
        private int LowLevelKeyboardProc(int nCode, UIntPtr wParam, ref InterceptKeys.KBDLLHOOKSTRUCT lParam)
        {
            string chars = "";

            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)
                {
                    // Captures the character(s) pressed only on WM_KEYDOWN
                    chars = InterceptKeys.VKCodeToString((uint)lParam.vkCode,
                        (wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_KEYDOWN ||
                        wParam.ToUInt32() == (int)InterceptKeys.KeyEvent.WM_SYSKEYDOWN));

                    hookedKeyboardCallbackAsync.BeginInvoke((InterceptKeys.KeyEvent)wParam.ToUInt32(), lParam.vkCode, chars, null, null);
                }

            //---My Block
            uint iwParam = wParam.ToUInt32();
            Debug.WriteLine(iwParam);
            if (iwParam == 256) // key down message
            {
                if (lParam.vkCode == 162) // control key down
                {
                    ctrl_pressed = true;
                }
            }
            if (iwParam == 257) // key up message
            {
                if (lParam.vkCode == 162) // control key up
                {
                    ctrl_pressed = false;
                }
            }

            bool forbidden = ForbiddenInput(lParam.vkCode, lParam.flags);
            if (ForbiddenInput(lParam.vkCode, lParam.flags))
                return 1;
            //---end

            return InterceptKeys.CallNextHookEx(hookId, nCode, wParam, ref lParam);
        }
Example #4
0
 /// <summary>
 /// Disposes the hook.
 /// <remarks>This call is required as it calls the UnhookWindowsHookEx.</remarks>
 /// </summary>
 public void Dispose()
 {
     InterceptKeys.UnhookWindowsHookEx(hookId);
 }
Example #5
0
        /// <summary>
        /// HookCallbackAsync procedure that calls accordingly the KeyDown or KeyUp events.
        /// </summary>
        /// <param name="keyEvent">Keyboard event</param>
        /// <param name="vkCode">VKCode</param>
        /// <param name="character">Character as string.</param>
        void KeyboardListener_KeyboardCallbackAsync(InterceptKeys.KeyEvent keyEvent, int vkCode, string character)
        {
            switch (keyEvent)
            {
                // KeyDown events
                case InterceptKeys.KeyEvent.WM_KEYDOWN:
                    if (KeyDown != null)
                        dispatcher.BeginInvoke(new RawKeyEventHandler(KeyDown), this, new RawKeyEventArgs(vkCode, false, character));
                    break;
                case InterceptKeys.KeyEvent.WM_SYSKEYDOWN:
                    if (KeyDown != null)
                        dispatcher.BeginInvoke(new RawKeyEventHandler(KeyDown), this, new RawKeyEventArgs(vkCode, true, character));
                    break;

                // KeyUp events
                case InterceptKeys.KeyEvent.WM_KEYUP:
                    if (KeyUp != null)
                        dispatcher.BeginInvoke(new RawKeyEventHandler(KeyUp), this, new RawKeyEventArgs(vkCode, false, character));
                    break;
                case InterceptKeys.KeyEvent.WM_SYSKEYUP:
                    if (KeyUp != null)
                        dispatcher.BeginInvoke(new RawKeyEventHandler(KeyUp), this, new RawKeyEventArgs(vkCode, true, character));
                    break;

                default:
                    break;
            }
        }