C#低级键盘挂钩不工作

时间:2009-11-21 20:24:00

标签: c# keyboard hook

这是我的keyhooking类的代码,但它不起作用。我想知道是否有人可以告诉我为什么?我正在另一个控制台应用程序中实现它。调试消息提供正确的输出,但键盘钩子根本不捕获键。我希望有人能告诉我原因。

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);
    }
} 

1 个答案:

答案 0 :(得分:13)

您是否在Application.Run函数中调用了Main

标准的Console线程没有消息循环,这是钩子正常工作所必需的,Application.Run负责处理。