SetWindowsHookEx函数全局键盘钩子没抓所有的按键 [英] SetWindowsHookEx global keyboard hook not catching all keypresses

查看:2720
本文介绍了SetWindowsHookEx函数全局键盘钩子没抓所有的按键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写代码,这是一个自动化系统的一部分。我想补充一个键盘钩子提前结束了测试,并且我用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:


  1. 我SetWindowsHookEx函数不正确,或
  2. 东西自动化系统在绕过我的键盘钩子(我真的不知道该怎么告诉虽然)。
  1. My SetWindowsHookEx isn't correct, or
  2. Something in the automation system is bypassing my keyboard hook (I don't really know how to tell though).

我已经写了使用示例代码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?

推荐答案

在回答您的评论 -

Responding to your comment -

如果你只需要测试键是否已关闭,以退出测试,你可以只轮询 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函数全局键盘钩子没抓所有的按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆