监听一个键时,应用程序不集中应用程序

由网友(少年为老心已荒凉)分享简介:我有一个应用程序(C#4.0的WPF),这是隐藏的,可以通过点击系统托盘图标或在其他框架我创建(小帧,它停靠左和最上面的)。I've an application(C# 4.0-WPF), which is hidden and can be displayed by clicking on the systray...

我有一个应用程序(C#4.0的WPF),这是隐藏的,可以通过点击系统托盘图标或在其他框架我创建(小帧,它停靠左和最上面的)。

I've an application(C# 4.0-WPF), which is hidden and can be displayed by clicking on the systray icon or on an other frame I created(small frame which is docked left and topmost).

我的客户希望增加一个新的方式来显示应用程序:当一个F键pressing(如 F9 )

My customer wants to add a new way to display the application: When pressing on a "F" key (e.g. F9).

我怎么知道我的应用程序,如果用户presses此键时,应用程序不是当前窗口/或不集中?

How do I know in my application if the user presses this key when the application is not the current window /or is not focused?

推荐答案

全局键盘钩子是不是正确的解决方案,如果你只想要几个全局热键。

Global keyboard hooks are not the right solution if you only want a few global hotkeys.

使用 WH_KEYBOARD 全局键盘钩子意味着您的DLL将被注入到每一个接收键presses过程。它不应该被用在所有的管理code中,由于在CLR是相对较重的重量,并可能导致版本冲突。

A global keyboard hook using WH_KEYBOARD means that your dll will be injected into every process that receives key presses. It should not be used at all in managed code, since the CLR is relatively heavy weight and may cause version conflicts.

这code注射液,也期待可疑的反病毒软件,这可能会阻止它。

This code injection will also look suspicious to anti-virus software, which might block it.

使用低级别的键盘钩子 WH_KEYBOARD_LL 是管理code一个更好的选择,因为它处理你自己的应用程序中的键盘事件。不过它要求每一个键盘事件是由你的线程来处理。

A low level keyboard hook using WH_KEYBOARD_LL is a better choice for managed code, since it processes the keyboard events in your own application. Still it requires that every keyboard event is handled by your thread.

这增加了键为pressed和目标应用程序接收它的时间。

This increases the time between a key being pressed and the target application receiving it.

这是特别坏,如果具有较高的CPU优先级的应用程序比你的应用程序饿死的CPU时间。在这种情况下,延迟可以达到几秒钟,群聚许多关键presses在一起。有些(写的不好)游戏作品一样,和无法播放在这种情况下。

This is particularly bad if an application with higher CPU priority than your application starves it of CPU time. In that case the latency can reach several seconds, bunching many keypresses together. Some (badly written) games work like that and become unplayable in such a situation.

Windows API函数RegisterHotKey是应有的功能用于全局热键。

The windows API function RegisterHotKey is the proper function to use for global hotkeys.

Windows将做过滤你的,只有通知您的应用程序,如果注册的钥匙之一是pressed,所以你不必处理所有的人。

Windows will do the filtering for you, and only notify your application if one of the registered keys has been pressed, so you don't have to process all of them.

使用一个简单的F-Key作为全局热键,你打算做,是有问题的,因为它经常发生冲突与具有焦点的应用程序的本地热键。所以,你应该做全局热键配置,因此用户能够避免碰撞与他们常用的应用程序。

Using a simple F-Key as global hotkey, as you plan on doing, is problematic since it often collides with a local hotkey of the application that has focus. So you should make global hotkeys configurable, so the user can avoid collisions with their commonly used applications.

阅读全文

相关推荐

最新文章