我被卡住了。在.NET中註冊熱鍵 - 三鍵/四鍵組合
現在,我使用下面的代碼來聽熱鍵:
[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);
}
爲什麼不是更有野心,去5或6組合鍵? – 2011-01-20 21:18:19
[RegisterHotKey註冊多個熱鍵](http:// stackoverflow。com/questions/4704134/register-more-one-hotkey-with-registerhotkey) – 2011-01-20 21:38:32
@David Heffernan,我會選擇5或6個組合鍵,但windows不允許同時點擊4個以上的鍵: P.無論如何,我只想涵蓋所有選項... – Ron 2011-01-20 21:56:12