全球Windows键preSS全球、Windows、preSS

2023-09-02 21:33:20 作者:倾我一生一世念

我有一个简单的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.

推荐答案

没有必要键盘挂钩。只需使用RegisterHotKey (定义一个系统范围内的热键)和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:

全局热键:寄存器,甚至触发热键时,形式不集中。 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是在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)