1

I am using the code below but with little modification:

class globalKeyboardHook {

    public struct keyboardHookStruct {
        public int vkCode;
        public int scanCode;
        public int flags;
        public int time;
        public int dwExtraInfo;
    }

    public delegate int keyboardHookProc(int code, int wParam, ref keyboardHookStruct lParam);

    public keyboardHookProc hookP;

    const int WH_KEYBOARD_LL = 13;
    const int WM_KEYDOWN = 0x100;
    const int WM_KEYUP = 0x101;
    const int WM_SYSKEYDOWN = 0x104;
    const int WM_SYSKEYUP = 0x105;
    #endregion


    public List<Keys> HookedKeys = new List<Keys>();
    IntPtr hhook = IntPtr.Zero;


    public event KeyEventHandler KeyDown;

    public event KeyEventHandler KeyUp;


    public globalKeyboardHook() {
        hook();
    }


    ~globalKeyboardHook() {
        unhook();
    }



    public void hook() {
        IntPtr hInstance = LoadLibrary("User32");

        hookP = new keyboardHookProc(hookProc);

        hhook = SetWindowsHookEx(WH_KEYBOARD_LL, hookP, hInstance, 0);
    }


    public void unhook() {
        UnhookWindowsHookEx(hhook);
    }

    public int hookProc(int code, int wParam, ref keyboardHookStruct lParam) {
        if (code >= 0) {
            Keys key = (Keys)lParam.vkCode;
            if (HookedKeys.Contains(key)) {
                KeyEventArgs kea = new KeyEventArgs(key);
                if ((wParam == WM_KEYDOWN || wParam == WM_SYSKEYDOWN) && (KeyDown != null)) {
                    KeyDown(this, kea) ;
                } else if ((wParam == WM_KEYUP || wParam == WM_SYSKEYUP) && (KeyUp != null)) {
                    KeyUp(this, kea);
                }
                if (kea.Handled)
                    return 1;
            }
        }
        return CallNextHookEx(hhook, code, wParam, ref lParam);
    }

    [DllImport("user32.dll")]
    static extern IntPtr SetWindowsHookEx(int idHook, keyboardHookProc callback, IntPtr hInstance, uint threadId);

    [DllImport("user32.dll")]
    static extern bool UnhookWindowsHookEx(IntPtr hInstance);

    [DllImport("user32.dll")]
    static extern int CallNextHookEx(IntPtr idHook, int nCode, int wParam, ref keyboardHookStruct lParam);

    [DllImport("kernel32.dll")]
    static extern IntPtr LoadLibrary(string lpFileName);

}

Sometimes after a few keypresses "hookProc" doesnt get called, as if i have "unhooked" the event, and it wont capture any keystrokes at all. How can i make sure that it never unhooks the event unless i do it in manually? How can i ensure that i keep subscribing to the hook?

Thank you.

brutton
  • 31
  • 1
  • 4
  • Are you ensuring that your hook is responding in a reaonable amount of time? Check out [this question](http://stackoverflow.com/questions/6728335/is-it-possible-to-detect-when-a-low-level-keyboard-hook-has-been-automatically-di/6729360#6729360) – Damien_The_Unbeliever Aug 01 '11 at 09:11
  • Good article, going to read it. – brutton Aug 01 '11 at 09:23

0 Answers0