我想为键盘钩子(Hook)创建一个好的库。我使用了 SetWindowsHookEx 方法,我注意到如果我的应用程序的主线程很忙,则不会执行应该在任何系统 KeyDown 事件中调用的方法 hookProc。我认为钩子(Hook)应该这样制作,另一个线程将对此负责。那可能吗?我该怎么做?
最佳答案
LowLevelKeyboardProc 的 Microsoft 帮助页面提到
If the hook procedure times out, the system passes the message to the next hook. However, on Windows 7 and later, the hook is silently removed without being called. There is no way for the application to know whether the hook is removed.
我怀疑这就是发生在你身上的事情。您的 HookProc 函数应该非常快:我所做的只是将关键事件推送到 std::vector 中。真正的代码在另一个线程中执行。
关于multithreading - 低级键盘 Hook 不在 UI 线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10054839/