C# 在后台捕获键盘按键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15413172/
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
Capture a keyboard keypress in the background
提问by ImonBayazid
I have a application that runs in the background. I have to generate some event whenever a user press F12at anytime. So what I need that to capture a key-press. In my application, if any time a user press F10some event will be performed. I don't understand how to do that?
我有一个在后台运行的应用程序。每当用户F12随时按下时,我都必须生成一些事件。所以我需要什么来捕捉按键。在我的应用程序中,如果任何时候用户按下F10某个事件都会执行。我不明白该怎么做?
Have anyone any idea how to do that?
有谁知道如何做到这一点?
N:B: It is a winforms application. It doesn't need to have focus my form. My main window may remain in system tray but still it have to capture the keypress.
N:B: 这是一个 winforms 应用程序。它不需要关注我的表单。我的主窗口可能保留在系统托盘中,但它仍然必须捕获按键。
采纳答案by Otiel
What you want is a global hotkey.
你想要的是一个全局热键。
Import needed libraries at the top of your class:
// DLL libraries used to manage hotkeys [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
Add a field in your class that will be a reference for the hotkey in your code:
const int MYACTION_HOTKEY_ID = 1;
Register the hotkey (in the constructor of your Windows Form for instance):
// Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8 // Compute the addition of each combination of the keys you want to be pressed // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6... RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12);
Handle the typed keys by adding the following method in your class:
protected override void WndProc(ref Message m) { if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) { // My hotkey has been typed // Do what you want here // ... } base.WndProc(ref m); }
在类的顶部导入所需的库:
// DLL libraries used to manage hotkeys [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
在类中添加一个字段,作为代码中热键的引用:
const int MYACTION_HOTKEY_ID = 1;
注册热键(例如在 Windows 窗体的构造函数中):
// Modifier keys codes: Alt = 1, Ctrl = 2, Shift = 4, Win = 8 // Compute the addition of each combination of the keys you want to be pressed // ALT+CTRL = 1 + 2 = 3 , CTRL+SHIFT = 2 + 4 = 6... RegisterHotKey(this.Handle, MYACTION_HOTKEY_ID, 6, (int) Keys.F12);
通过在类中添加以下方法来处理键入的键:
protected override void WndProc(ref Message m) { if (m.Msg == 0x0312 && m.WParam.ToInt32() == MYACTION_HOTKEY_ID) { // My hotkey has been typed // Do what you want here // ... } base.WndProc(ref m); }
回答by baron_bartek
In case you have problem running Otiel's solution:
如果您在运行 Otiel 的解决方案时遇到问题:
You need to include:
using System.Runtime.InteropServices; //required for dll import
Another doubt for newbies like me: "top of the class" really means top of your class like this (not namespace or constructor):
public partial class Form1 : Form { [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
You don't need to add user32.dll as reference to the project. WinForms always load this dll automatically.
您需要包括:
using System.Runtime.InteropServices; //required for dll import
像我这样的新手的另一个疑问:“一流的”实际上意味着像这样的一流(不是命名空间或构造函数):
public partial class Form1 : Form { [DllImport("user32.dll")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, int vlc); [DllImport("user32.dll")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
您不需要添加 user32.dll 作为对项目的引用。WinForms 总是自动加载这个 dll。