using System; using System.Collections.Generic; using System.Windows.Forms; using System.Runtime.InteropServices; public class HotKeys { int keyid = 10; Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>(); public delegate void HotKeyCallBackHanlder(); public enum HotkeyModifiers { Alt = 1, Control = 2, Shift = 4, Win = 8 } [DllImport("user32.dll")] static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk); [DllImport("user32.dll")] static extern bool UnregisterHotKey(IntPtr hWnd, int id); /// <summary> /// 注册快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="fsModifiers">组合键</param> /// <param name="vk">快捷键的虚拟键码</param> /// <param name="callBack">回调函数(按下快捷键时被调用的方法)</param> public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack) { int id = keyid++; if (!RegisterHotKey(hWnd, id, modifiers, vk)) throw new Exception("注册失败!"); keymap[id] = callBack; } /// <summary> /// 注销快捷键 /// </summary> /// <param name="hWnd">持有快捷键窗口的句柄</param> /// <param name="callBack">回调函数</param> public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack) { foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap) { if (var.Value == callBack) UnregisterHotKey(hWnd, var.Key); } } /// <summary> /// 快捷键消息处理 /// </summary> public void ProcessHotKey(Message m) { if (m.Msg == 0x312) { int id = m.WParam.ToInt32(); HotKeyCallBackHanlder callback; if (keymap.TryGetValue(id, out callback)) callback(); } } }
HotKeys h = new HotKeys(); //窗体登录时注册快捷键 private void Form2_Load(object sender, EventArgs e) { //这里注册了Ctrl+Alt+E 快捷键 h.Regist(this.Handle, (int)HotKeys.HotkeyModifiers.Control + (int)HotKeys.HotkeyModifiers.Alt, Keys.E, CallBack); MessageBox.Show("注册成功"); } //按钮点击卸载 private void button1_Click(object sender, EventArgs e) { h.UnRegist(this.Handle, CallBack); MessageBox.Show("卸载成功"); } //重载WndProc函数 protected override void WndProc(ref Message m) { h.ProcessHotKey(m);//快捷键消息处理 base.WndProc(ref m); } //按下快捷键时被调用的方法 public void CallBack() { MessageBox.Show("快捷键被调用!"); }