全球Windows键preSS [英] Global Windows Key Press

查看:221
本文介绍了全球Windows键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 (定义一个系统范围内的热键)和<一href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms646327.aspx"><$c$c>UnregisterHotKey从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:

  • 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屋!

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