代码之家  ›  专栏  ›  技术社区  ›  meds

setWindowsHookEx在C.NET核心中不工作?

  •  0
  • meds  · 技术社区  · 6 年前

    我正在使用以下代码尝试在没有运气的情况下获得操作系统范围的键盘输入:

    using System;
    
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    
    class InterceptKeys
    
    {
        private const int WH_KEYBOARD_LL = 13;
        private const int WM_KEYDOWN = 0x0100;
        private static LowLevelKeyboardProc _proc = HookCallback;
        private static IntPtr _hookID = IntPtr.Zero;
    
        public static void Main()
    
        {
            _hookID = SetHook(_proc);
    
            while(true)
                continue;
        }
    
        private static IntPtr SetHook(LowLevelKeyboardProc proc)
        {
            using (Process curProcess = Process.GetCurrentProcess())
    
            using (ProcessModule curModule = curProcess.MainModule)
            {
                return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                    GetModuleHandle(curModule.ModuleName), 0);
            }
        }
        private delegate IntPtr LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);
        private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
        {
            if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
            {
                int vkCode = Marshal.ReadInt32(lParam);
                Console.WriteLine(vkCode);
            }
    
            return CallNextHookEx(_hookID, nCode, wParam, lParam);
        }
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
    
        private static extern bool UnhookWindowsHookEx(IntPtr hhk);
    
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
        private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
    
    
        [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    
        private static extern IntPtr GetModuleHandle(string lpModuleName);
    
    }
    

    HookCallback只是没有被调用。我有一种怀疑,它只是在尝试只听一种不存在的形式,而不是在系统范围内运行。

    1 回复  |  直到 6 年前
        1
  •  2
  •   Alex F    6 年前

    低级Windows挂钩在内部使用Windows消息传递。调用的线程 SetWindowsHookEx 最后必须有消息循环,允许调用 HookCallback 功能。在C++消息循环中是这样的:

    MSG msg;
    BOOL result;
    
    for (;;)
    {
        result = GetMessage(&msg, nullptr, 0, 0);
    
        if (result <= 0)
        {
            break;
        }
    
        TranslateMessage(&msg);
        DispatchMessage(&msg);
    }
    

    查找所有必需的pinvoke定义 GetMessage , TranslateMessage , DispatchMessage MSG ,将此代码转换为c并将其放置,而不是无限循环 while(true) . 您可以在pinvoke.net上找到所有这些内容,另请参阅此Microsoft论坛讨论:

    控制台键盘挂钩未被调用

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/ed5be22c-cef8-4615-a625-d05caf113afc/console-keyboard-hook-not-getting-called?forum=csharpgeneral