gpt4 book ai didi

c# - SetWindowsHookEx(WH_KEYBOARD) 只产生第一个事件,顺序事件

转载 作者:行者123 更新时间:2023-11-30 22:40:20 27 4
gpt4 key购买 nike

我希望从 .net 应用程序中捕获全局的所有键盘事件。

我使用 win32 方法 SetWindowsHookEx(WH_KEYBOARD, HINSTANCE) 设置回调。 (使用 dllimport 和一些 mashall-call。)

在按下第一个键时,我会得到一个很好的响应,说明按下了哪个键。
我的回调函数按原样调用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com