我希望从 .net 应用程序捕获全局的所有键盘事件。
我使用 win32 方法设置回调 SetWindowsHookEx(WH_KEYBOARD, HINSTANCE)
。 (使用 dllimport 和一些 mashall 调用。)
按下第一个键后,我会得到一个很好的响应,说明按下了哪个键。
我的回调函数调用 CallNextHookEx
应该这样做。
但是在第一个事件之后我就没有再收到任何事件。
有人知道事件停止发生的常见原因吗?
最佳答案
托管包装器不支持
WH_KEYBOARD
,它需要将自身注入(inject)到进程中。
您可以使用将在声明线程中调用的WH_KEYBOARD_LL
。
参见:http://support.microsoft.com/kb/318804
Global hooks are not supported in the .NET Framework Except for the WH_KEYBOARD_LL low-level hook and the WH_MOUSE_LL low-level hook, you cannot implement global hooks in the Microsoft .NET Framework. To install a global hook, a hook must have a native DLL export to inject itself in another process that requires a valid, consistent function to call into. This behavior requires a DLL export. The .NET Framework does not support DLL exports. Managed code has no concept of a consistent value for a function pointer because these function pointers are proxies that are built dynamically.
Low-level hook procedures are called on the thread that installed the hook. Low-level hooks do not require that the hook procedure be implemented in a DLL.
关于c# - SetWindowsHookEx(WH_KEYBOARD) 仅产生第一个事件、顺序事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5264288/