在 C# 中处理全局热键处理的最佳方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/81150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 11:19:11  来源:igfitidea点击:

Best way to tackle global hotkey processing in c#?

提问by

Possible Duplicate:
How can I register a global hot key to say CTRL+SHIFT+(LETTER) using WPF and .NET 3.5?

可能的重复:
如何使用 WPF 和 .NET 3.5 注册全局热键来说 CTRL+SHIFT+(LETTER)?

I'd like to have multiple global hotkeys in my new app (to control the app from anywhere in windows), and all of the given sources/solutions I found on the web seem to provide with a sort of a limping solution (either solutions only for one g.hotkey, or solutions that while running create annoying mouse delays on the screen).

我想在我的新应用程序中有多个全局热键(以从 Windows 中的任何位置控制应用程序),并且我在网上找到的所有给定来源/解决方案似乎都提供了一种跛行解决方案(任一解决方案仅适用于一个 g.hotkey,或者在运行时会在屏幕上产生烦人的鼠标延迟的解决方案)。

Does anyone here know of a resource that can help me achive this, that I can learn from? Anything?

这里有没有人知道可以帮助我实现这一目标的资源,我可以从中学习?任何事物?

Thanks ! :)

谢谢 !:)

回答by arul

回答by Andy

I would handle this by using P/Invoke to call RegisterHotKey() for each hotkey, and then use NativeForm (assuming you are using WinForms) to be notified of the WM_HOTKEY message. This should keep most of your hotkey code in one place.

我将通过使用 P/Invoke 为每个热键调用 RegisterHotKey() 来处理这个问题,然后使用 NativeForm(假设您使用的是 WinForms)来接收 WM_HOTKEY 消息的通知。这应该将您的大部分热键代码保存在一个地方。

回答by Ohad Schneider

The nicest solution I've found is http://bloggablea.wordpress.com/2007/05/01/global-hotkeys-with-net/

我找到的最好的解决方案是http://bloggablea.wordpress.com/2007/05/01/global-hotkeys-with-net/

Hotkey hk = new Hotkey();

hk.KeyCode = Keys.1;
hk.Windows = true;
hk.Pressed += delegate { Console.WriteLine("Windows+1 pressed!"); };

hk.Register(myForm); 

Note how you can set different lambdas to different hotkeys

请注意如何将不同的 lambdas 设置为不同的热键