全球Windows键preSS [英] Global Windows Key Press
问题描述
我有一个简单的WPF应用程序,我需要捕捉pssed在Windows(操作系统),F1键$ P $即使我的WPF窗口最小化,或者没有被激活。
I have a simple WPF application and I need to capture F1 key pressed in Windows (Operation System), even if my WPF window is minimized, or it isn't activated.
我有问题,检测这一点。我搜索在互联网上,我发现了很多成果,但他们并没有帮助我。
I have problems with detecting this. I searched on Internet and I found many results, but they didn't helped me.
有关检测的关键$ P $应用程序内部pssed我用这个简单的code:
For detecting a key pressed inside of application I used this simple code:
AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)KeyPressed);
private void KeyPressed(object sender, KeyEventArgs e)
{
if (e.Key == Key.F1)
{
//my code went here
}
}
但是,这时候我的窗口没有被激活无法正常工作。
But this doesn't work when my window isn't activated.
所以,我的问题是:如何检测全局键preSS
我再说一遍:这是一个WPF应用程序
I repeat: It is a WPF application.
推荐答案
没有必要键盘挂钩
。只需使用<一个href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646309.aspx"><$c$c>RegisterHotKey$c$c> (定义一个系统范围内的热键)和<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646327.aspx"><$c$c>UnregisterHotKey$c$c>从Windows API。尝试使用这些在C#中的 pinvoke.net 或这些教程:
No need to Keyboard Hooking
. Just use RegisterHotKey
(Defines a system-wide hot key) and UnregisterHotKey
from Windows API. Try using these in C# from pinvoke.net or these tutorials:
- 全局热键:寄存器,甚至触发热键时,形式不集中。
- <一个href="http://www.$c$cproject.com/Articles/5914/Simple-steps-to-enable-Hotkey-and-ShortcutInput-us">Simple步骤启用热键和ShortcutInput用户控制
- Global Hotkeys: Register a hotkey that is triggered even when form isn't focused.
- Simple steps to enable Hotkey and ShortcutInput user control
Thse是在<一个样本href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/87d66b1c-330c-42fe-8a40-81f82012575c">Microsoft论坛的。
您可以使用这些改性剂和虚拟键codeS
You can use these modifiers and Virtual-Key Codes:
MOD_ALT (0x0001)
MOD_CONTROL (0x0002)
MOD_NOREPEAT (0x4000)
MOD_SHIFT (0x0004)
MOD_WIN (0x0008)
例如F1键是 VK_F1(0x70)
。
这篇关于全球Windows键preSS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!