Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:

This doesn't work.

No error message or anything, it just doesn't work.

 Anyone know why?

    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;

        public InterceptKeys(String holder)
        {
        //    keysHooked = holder;
            Hook();
        }

        public void Hook()
        {
            IntPtr hInstance = LoadLibrary("User32");
            Console.WriteLine("hInstance: "+hInstance);
            _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, HookCallback, hInstance, 0);
            Console.WriteLine("_hookID: "+_hookID);
        }

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

        public delegate int HookProc(int code, int wParam, HookStruct lParam);

        public struct HookStruct {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;
        }
        
        public int HookCallback(int code, int wParam, HookStruct lParam) 
        {   
                if (code >= 0 && wParam == WM_KEYDOWN)
                {
                    int vkCode = lParam.vkCode;
                    Console.WriteLine((Keys)vkCode);
                }
            return CallNextHookEx(_hookID, code, wParam, lParam);
        }
        
        [DllImport("user32.dll")]
        static extern IntPtr SetWindowsHookEx(int idHook, HookProc 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, HookStruct lParam);

        [DllImport("kernel32.dll")]
        static extern IntPtr LoadLibrary(string lpFileName);
    }
Posted
Updated 11-Jul-17 3:16am
v2

Your HookCallback is being garbage collected as soon as Hook() is done executing. The garbage collector doesn't know about native code like SetWindowsHookEx is trying to reference your C# function.

To fix this, store a field in your class of type HookProc. In the Hook() method, set the field to HookCallback. Then pass in the field as the argument to SetWindowsHookEx.  This will keep your hook alive as long as InterceptKeys class instance is alive. 

 
Share this answer
 
v2

       private static HookProc KeyboardProc;


        public InterceptKeys(String holder)
        {
            Hook();
        }

        public void Hook()
        {
            IntPtr hInstance = LoadLibrary("User32");
            
            KeyboardProc = HookCallback;
            _hookID = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, hInstance, 0);

            if (_hookID == (IntPtr) 0)
            {
                Console.WriteLine("Failed to hook.");
            }
            Console.WriteLine("[Success!] _hookID: "+_hookID);
        }

 

This is the new code, hopefully I did what you asked. Still isn't working though. The hookID is working and everything, but still no keys being caught. I also tried

 KeyboardProc = new HookProc(HookCallback);

That didn't work either.

 
Share this answer
 

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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