我正在编写属于自动化系统一部分的代码.我想添加一个键盘钩子来提前结束测试,我通过使用 SetWindowHookEx 来做到这一点.
I'm writing code that is part of an automation system. I wanted to add a keyboard hook to end the test prematurely, and I did this by using SetWindowHookEx.
我的代码看起来很像这样:http://support.microsoft.com/kb/318804
My code looks pretty much like this: http://support.microsoft.com/kb/318804
这是我的 SetWindowsHookEx 调用:
Here's my SetWindowsHookEx call:
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardHookProcedure, GetModuleHandle(Process.GetCurrentProcess().MainModule.ModuleName), 0);
现在,当我运行自动化时,自动化系统内的按键(来自 SendKeys)会触发键盘挂钩方法,但是当我手动敲击键盘时它不会被触发.
Now, when I run my automation, keypresses from within the automation system (from SendKeys) trigger the keyboard hook method, but when I hit the keyboard manually it isn't triggered.
如果有帮助,我可以分享更多代码,但它是更大系统的一部分.我很确定:
I can share more code if that helps, but it's part of a much larger system. I'm pretty sure that either:
我编写了一个测试应用程序,该应用程序使用来自 microsoft.com 的示例代码来确定我的方法具有优点(即它有效),但我无法将它与自动化系统集成.
I've written a test application that uses the sample code from microsoft.com to determine that my approach has merit (i.e., it works), but I'm having trouble integrating it with the automation system.
任何关于如何确定哪里出了问题的想法都将不胜感激.
Any thoughts on how to determine where things are going wrong would be greatly appreciated.
自动化工具中没有其他 SetWindowsHookEx 实例.我不太清楚全局键盘挂钩 w.r.t 的细微差别.线程和桌面.如果我添加一个全局键盘钩子,它的添加位置是否重要?
There are no other instances of SetWindowsHookEx in the automation harness. I'm not too clear the nuances of global keyboard hooks w.r.t. threads and the desktop. If I add a global keyboard hook, should it matter from where it was added?
回复你的评论-
如果您只需要测试某个键是否已关闭以退出测试,您只需轮询 GetAsyncKeyState()
,这将告诉您某个特定的键是否已关闭,无论是谁当前具有键盘焦点.
If you just need to test whether a key is down in order to exit your test, you could just poll on GetAsyncKeyState()
, this will tell you if a particular key is down regardless of who currently has keyboard focus.
您的用户必须按住一个键或一组键足够长的时间才能让您的轮询注意到,这意味着他们按住它几秒钟,或者您必须更频繁地轮询一秒钟.
You user would have to hold a key or set of keys down long enough for your polling to notice, which means either they hold it down for a few seconds, or you have to poll more frequently than a second.
但这比全局键盘钩子要少得多.
But that would be a lot less intrusive than a global keyboard hook.
全局挂钩可能会序列化通常彼此异步的内核部分,因此它们也会损害系统性能.
Global hooks supposedly serialize parts of the kernel that normally would be async from each other, so they also harm system performance.
这篇关于SetWindowsHookEx 全局键盘挂钩未捕获所有按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!