如何在c#中获得组合键

如何在C#表单上捕获Ctrl + Alt + K + P键? 谢谢

这是一个和弦,如果没有记住和弦的第一次击键,就无法检测到它。 这有效:

public partial class Form1 : Form { public Form1() { InitializeComponent(); } private bool prefixSeen; protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (prefixSeen) { if (keyData == (Keys.Alt | Keys.Control | Keys.P)) { MessageBox.Show("Got it!"); } prefixSeen = false; return true; } if (keyData == (Keys.Alt | Keys.Control | Keys.K)) { prefixSeen = true; return true; } return base.ProcessCmdKey(ref msg, keyData); } } 

我不确定你能不能。 但是,你可以做的是Visual Studio的工作方式。 它有快捷键,如Ctrl + KC 。 首先按Ctrl + K ,然后按住Ctrl并按C键。 在您的情况下,您可以检查Ctrl + Alt + KP

您可以先在其他答案中检查Ctrl + Alt + K ,然后设置成员变量/标志以指示已按下Ctrl + Alt + K. 在检查K的同一方法中,您可以检查P ,如果您刚设置的标志设置为true,则执行您需要执行的操作。 否则将标志设置为false。

粗伪伪代码:

 private bool m_bCtrlAltKPressed = false; public void KeyDown() { if (Ctrl+Alt+K) { m_bCtrlAltKPressed = true; } else if (Ctrl+Alt+P && m_bCtrlAltKPressed) { //do stuff } else { m_bCtrlAltKPressed = false; } } 

希望足够清楚。

在这种情况下, MessageFilters可以帮助您。

  public class KeystrokMessageFilter : System.Windows.Forms.IMessageFilter { public KeystrokMessageFilter() { } #region Implementation of IMessageFilter public bool PreFilterMessage(ref Message m) { if ((m.Msg == 256 /*0x0100*/)) { switch (((int)m.WParam) | ((int)Control.ModifierKeys)) { case (int)(Keys.Control | Keys.Alt | Keys.K): MessageBox.Show("You pressed ctrl + alt + k"); break; //This does not work. It seems you can only check single character along with CTRL and ALT. //case (int)(Keys.Control | Keys.Alt | Keys.K | Keys.P): // MessageBox.Show("You pressed ctrl + alt + k + p"); // break; case (int)(Keys.Control | Keys.C): MessageBox.Show("You pressed ctrl+c"); break; case (int)(Keys.Control | Keys.V): MessageBox.Show("You pressed ctrl+v"); break; case (int)Keys.Up: MessageBox.Show("You pressed up"); break; } } return false; } #endregion } 

现在在C#WindowsForm中,注册MessageFilter以捕获键击和组合。

 public partial class Form1 : Form { KeystrokMessageFilter keyStrokeMessageFilter = new KeystrokMessageFilter(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Application.AddMessageFilter(keyStrokeMessageFilter); } } 

不知何故,它只检测Ctrl + Alt + K. 请阅读MessageFilter代码中的注释。

看到这篇关于在c#中设置热键的精彩博文

  • Christian Liensberger»博客»使用C#安装全局热键

还有一篇很好的文章解释了这一切

  • 启用Hotkey和ShortcutInput用户控件的简单步骤 – CodeProject

当按下组合中的某个键时,可以使用GetKeyState获取其他键的状态。 这是表格上的一个例子。

 using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; namespace DetectKeyChord { public partial class Form1 : Form { private const int WM_KEYDOWN = 0x100; private const int KEY_PRESSED = 0x80; public Form1() { InitializeComponent(); } public void ShortcutAction() { MessageBox.Show("Ctrl+Alt+K+P has been pressed."); } private bool IsKeyDown(Keys key) { return (NativeMethods.GetKeyState(key) & KEY_PRESSED) == KEY_PRESSED; } protected override void WndProc(ref Message m) { if (m.Msg == WM_KEYDOWN) { //If any of the keys in the chord have been pressed, check to see if //the entire chord is down. if (new[] { Keys.P, Keys.K, Keys.ControlKey, Keys.Menu }.Contains((Keys)m.WParam)) { if (IsKeyDown(Keys.ControlKey) && IsKeyDown(Keys.Menu) && IsKeyDown(Keys.K) && IsKeyDown(Keys.P)) { this.ShortcutAction(); } } } base.WndProc(ref m); } } public static class NativeMethods { [DllImport("USER32.dll")] public static extern short GetKeyState(Keys nVirtKey); } }