您想要的是 全局热键 。
在类的顶部导入所需的库:
// 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 pres: 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);}



