简体   繁体   English

如何确定全局键盘挂钩是否已断开连接?

[英]How to make sure if global keyboard hook have been disconnected?

I have used Stephen Toubs version of global keyboard hook, using it in a WIN 7 environment. 我使用了Stephen Toubs版本的全局键盘钩子,在WIN 7环境中使用它。

It works fine for a moment, and then it just stops receiving keystrokes. 它工作正常,然后它就停止接收击键。

I have increased the timeout value in regedit but it still disconnects after some time. 我在regedit中增加了超时值,但在一段时间后它仍然断开连接。

So problem is, how do I detect if it have been disconnected and how do I set the hook to be active again? 问题是,如何检测它是否已断开连接以及如何将钩子设置为再次激活?

There is no way of knowing (from msdn ): 没有办法知道(来自msdn ):

The hook procedure should process a message in less time than the data entry specified in the LowLevelHooksTimeout value in the following registry key: HKEY_CURRENT_USER\\Control Panel\\Desktop 钩子过程应该在比以下注册表项中的LowLevelHooksTimeout值中指定的数据条目更短的时间内处理消息:HKEY_CURRENT_USER \\ Control Panel \\ Desktop

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. 但是,在Windows 7及更高版本中,无需调用即可静默删除挂钩。 There is no way for the application to know whether the hook is removed. 应用程序无法知道钩子是否被移除。

One way you can work around this is to keep the timestamp of last message received and use background timer to add new hook after some time has passed from previous message. 解决此问题的一种方法是保留收到的最后一条消息的时间戳,并使用后台计时器在从上一条消息过去一段时间后添加新的挂钩。

Also, to minimize this happening your hook handling method should only collect minimum required information and leave the actual processing of the data to another thread. 此外,为了最大限度地减少这种情况,您的钩子处理方法应该只收集最少的所需信息,并将数据的实际处理留给另一个线程。

This is a feature! 这是一个功能! here's the description of the reason . 是对原因描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM