c# 注册全局热键

//引入系统API
[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); int keyid = ; //区分不同的快捷键
Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>(); //每一个key对于一个处理函数
public delegate void HotKeyCallBackHanlder(); //组合控制键
public enum HotkeyModifiers
{
Alt = ,
Control = ,
Shift = ,
Win =
} //注册快捷键
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;
} // 注销快捷键
public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
{
if (var.Value == callBack)
{
UnregisterHotKey(hWnd, var.Key);
return;
}
}
} // 快捷键消息处理
public void ProcessHotKey(Message m)
{
if (m.Msg == 0x312)
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
callback();
}
}
public Form1()
{
InitializeComponent();
} HotKeys h = new HotKeys(); private void button1_Click(object sender, EventArgs e)
{
if (button1.Text == "注册")
{
string vaule = textBox1.Text;
Regist(vaule);
button1.Text = "卸载";
label2.Text = "注册成功";
}
else
{
h.UnRegist(this.Handle, CallBack);
button1.Text = "注册";
label2.Text = "卸载成功";
}
} private void Regist(string str)
{
if (str == "")
return;
int modifiers = ;
Keys vk = Keys.None;
foreach (string value in str.Split('+'))
{
if (value.Trim() == "Ctrl")
modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Control;
else if (value.Trim() == "Alt")
modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Alt;
else if (value.Trim() == "Shift")
modifiers = modifiers + (int)HotKeys.HotkeyModifiers.Shift;
else
{
if (Regex.IsMatch(value, @"[0-9]"))
{
vk = (Keys)Enum.Parse(typeof(Keys), "D" + value.Trim());
}
else
{
vk = (Keys)Enum.Parse(typeof(Keys), value.Trim());
}
}
}
//这里注册了Ctrl+Alt+E 快捷键
h.Regist(this.Handle, modifiers, vk, CallBack);
} //按下快捷键时被调用的方法
public void CallBack()
{
label2.Text = "快捷键被调用!";
} protected override void WndProc(ref Message m)
{
//窗口消息处理函数
h.ProcessHotKey(m);
base.WndProc(ref m);
} private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
StringBuilder keyValue = new StringBuilder();
keyValue.Length = ;
keyValue.Append("");
if (e.Modifiers != )
{
if (e.Control)
keyValue.Append("Ctrl + ");
if (e.Alt)
keyValue.Append("Alt + ");
if (e.Shift)
keyValue.Append("Shift + ");
}
if ((e.KeyValue >= && e.KeyValue <= ) ||
(e.KeyValue >= && e.KeyValue <= ) || //a-z/A-Z
(e.KeyValue >= && e.KeyValue <= )) //F1-F12
{
keyValue.Append(e.KeyCode);
}
else if ((e.KeyValue >= && e.KeyValue <= )) //0-9
{
keyValue.Append(e.KeyCode.ToString().Substring());
}
((TextBox)sender).Text = keyValue.ToString();
} private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
string str = ((TextBox)sender).Text.TrimEnd();
int len = str.Length;
if (len >= && str.Substring(str.Length - ) == "+")
{
((TextBox)sender).Text = "";
}
} private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
h.UnRegist(this.Handle, CallBack);
}

界面

c# 注册全局热键

c# 注册全局热键

c# 注册全局热键

上一篇:js简单的设置快捷键,hotkeys捕获键盘键和组合键的输入


下一篇:System.Diagnostics.Process.Star的用法