none
Global texthook won't let me type RRS feed

  • Question

  • Hello everyone,

     I have this code that lets me get the keys pressed globally in c#. However, other programs won't accept keyboard input while it is hooked. For instance, if it is hooked, It can sense if t is pressed but t will not be typed into a program (like a text editor), or any other character for the matter. My code is below:

            [DllImport("user32.dll")]
            static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc callback, IntPtr hInstance, uint threadId);
    
            [DllImport("user32.dll")]
            static extern bool UnhookWindowsHookEx(IntPtr hInstance);
    
            [DllImport("user32.dll")]
            static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, int wParam, IntPtr lParam);
    
            [DllImport("kernel32.dll")]
            static extern IntPtr LoadLibrary(string lpFileName);
    
            private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
    
            const int WH_KEYBOARD_LL = 13;
            const int WM_KEYDOWN = 0x100;
            private LowLevelKeyboardProc _proc = hookProc;
    
            private static IntPtr hhook = IntPtr.Zero;
    
            public void SetHook()
            {
                IntPtr hInstance = LoadLibrary("User32");
                hhook = SetWindowsHookEx(WH_KEYBOARD_LL, _proc, hInstance, 0);
            }
    
            public static void UnHook()
            {
                UnhookWindowsHookEx(hhook);
            }
    
            public static IntPtr hookProc(int code, IntPtr wParam, IntPtr lParam)
            {
                if (code >= 0 && wParam == (IntPtr)WM_KEYDOWN)
                {
                    int vkCode = Marshal.ReadInt32(lParam);
                    if (vkCode.ToString() == "13") //enter
                    {
                        //MessageBox.Show("You pressed the ENTER button");
                        new Form1().enterPressed();
                    }
                    return (IntPtr)1;
                }
                else
                    return CallNextHookEx(hhook, code, (int)wParam, lParam);
            }
    
            private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                UnHook();
    
            }

    Any ideas why this is happening and what I can to do make it able to allow typing while still sensing what key is pressed?

    Saturday, May 5, 2018 8:44 PM

Answers

  • You must not return 1 in the hookProc

    (see MSDN :

    "If the hook procedure processed the message, it may return a nonzero
    value to prevent the system from passing the message to the rest of the
    hook chain or the target window procedure. ")

    • Marked as answer by Wilkyrl Saturday, May 5, 2018 10:19 PM
    Saturday, May 5, 2018 9:09 PM
  • Actually, I believe I solved it. What I did was replace: 

     return (IntPtr)1;

    with

                    return CallNextHookEx(hhook, code, (int)wParam, lParam);

    • Marked as answer by Wilkyrl Saturday, May 5, 2018 10:19 PM
    Saturday, May 5, 2018 10:19 PM

All replies

  • You must not return 1 in the hookProc

    (see MSDN :

    "If the hook procedure processed the message, it may return a nonzero
    value to prevent the system from passing the message to the rest of the
    hook chain or the target window procedure. ")

    • Marked as answer by Wilkyrl Saturday, May 5, 2018 10:19 PM
    Saturday, May 5, 2018 9:09 PM
  • Any idea what I should do? I'm a little out of my league.
    Saturday, May 5, 2018 10:14 PM
  • Actually, I believe I solved it. What I did was replace: 

     return (IntPtr)1;

    with

                    return CallNextHookEx(hhook, code, (int)wParam, lParam);

    • Marked as answer by Wilkyrl Saturday, May 5, 2018 10:19 PM
    Saturday, May 5, 2018 10:19 PM