Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Low-Level Keyboard Hook Not Working

Tags:

c#

keyboard

hook

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"Wink;
            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);
    }
} 
like image 441
Overload119 Avatar asked Dec 08 '22 05:12

Overload119


1 Answers

Are you calling Application.Run in your Main function?

The standard Console thread doesn't have a message loop, which is required for hooks to work properly, Application.Run takes care of that.

like image 197
arul Avatar answered Jan 05 '23 04:01

arul