开发者

SetWindowsHookEx(WH_KEYBOARD) only yields first event, sequential events

From a .net application I wish to capture all keyboard events, globally.

I set a callback using a win32-method SetWindowsHookEx(WH_KEYBOARD, HINSTANCE). (Using dllimport and some mashall-call.)

One the fi开发者_如何学Pythonrst key is pressed I get a nice response saying which key is pressed.

My callback-function calls CallNextHookEx as is should do.

But after the first event I get no more events.

Any-one has any idea of common causes for events to stop coming?


WH_KEYBOARD is not supported from a managed wrapper, it needs to inject itself into the process.

You can use WH_KEYBOARD_LL which will be called in the declaring thread.

see: 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.


If your hook callback function takes too long to return, windows will stop calling it to preserve system performance. Try just a simple call to OutputDebugString and CallNextHookEx and see if that helps...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜