我被困。
现在,我正在使用以下代码来收听热键:
[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);
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
// whatever i need
}
base.WndProc(ref m);
}
和这个注册热键的功能:
Form1.RegisterHotKey(this.Handle, this.GetType().GetHashCode(), 0, (int)chr);
它完美地工作。我的问题是如何将多个热键注册为相同的组合,例如:
- A+B+C+D
- ALT+SHIFT+B
- CTRL+ALT+SHIFT+X
编辑:我发现(就像 Zooba 说的)如何“解密”发送了哪个热键,这是解决方案:
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0312)
{
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
if ((modifier + "+" + key == "Alt+S"))
{
//do what ever I need.
}
}
base.WndProc(ref m);
}