Click here to Skip to main content
15,443,631 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

 

This is the code for my keyhooking class, but it doesn't work. I was wondering if someone can tell me why? I'm instansiating it in another Console application. The debug message gives the proper output, but the keyboard hook simply doesn't catch keys. I was hoping if someone could tell me why.

 

namespace GlobalHooks
{
    public class InterceptKeys
    {        
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;

        private static IntPtr _hookID = IntPtr.Zero;
        private static String keysHooked = String.Empty;
        
        private static LowLevelHookProc keyboardHook;
       
        public delegate IntPtr LowLevelHookProc(int nCode, Int32 wParam, IntPtr lParam);
        public delegate void KeyboardHandleFunction(int vkCode);
        public static event KeyboardHandleFunction keyHookReturn;

        public InterceptKeys(KeyboardHandleFunction func)
        {
            keyHookReturn = func;
            keyboardHook = new LowLevelHookProc(HookCallback);
        }

        public static void debug()
        {
            Console.Write("\n[Success!] _hookID: "+_hookID);
            Console.Write("\n[Success!] keyboardProc: "+keyboardHook.ToString());
        }

        private IntPtr SetupHook(LowLevelHookProc keyProcess)
        {
            using (Process curProcess = Process.GetCurrentProcess())
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, keyProcess,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }

        public void Hook()
        {
            _hookID = SetupHook(keyboardHook);
            debug();
        }

        public void Unhook()
        {
            UnhookWindowsHookEx(_hookID);
        }


        public static void OnCallbackReturn(int nCode)
        {
            if (keyHookReturn != null)
            {
                keyHookReturn(nCode);
            }
            else
            {
                throw new Exception();
            }
        }

        public static IntPtr HookCallback(int nCode, Int32 wParam, IntPtr lParam)
        {
            Console.WriteLine("Calledback");
            if (nCode >= 0 && wParam == WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine((Keys)vkCode);
                OnCallbackReturn(nCode);
            }
            return CallNextHookEx((int)_hookID, nCode, wParam, lParam);
        }
       
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelHookProc lpfn, IntPtr hMod, uint dwThreadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(int hhk, int nCode, int wParam, IntPtr lParam);

        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    }
}

Posted
Comments
BillWoodruff 23-Nov-14 8:10am    
If this is not code you wrote (nothing wrong with that, by the way), can you give us a link to where you obtained it, or is there a source you used as a basis for this you could give us a link to. If the code is from one of the CP articles on GlobalHook that would be useful to know.

1 solution

This is not a direct answer to your question, but here is a good article that might help you along.
Global System Hooks in .NET[^]

And this is an alternative to hooks:
Using Raw Input from C# to handle multiple keyboards[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900