定义快捷键的方法
//在form_load的时候写上 //表示窗体接受按键事件 //然后在KeyDown事件中写入如下代码实现输入的是否是Ctrl+A的组合键: private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.A && e.Modifiers == Keys.Control) MessageBox.Show("Ctrl+R"); } //验证输入的是不是组合键盘Ctrl+A protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == (Keys.Control | Keys.A)) { MessageBox.Show("Ctr+A"); } return base.ProcessCmdKey(ref msg, keyData); } //或者使用一般按键处理也就可以了 protected override bool ProcessDialogKey(Keys keyData) { if (keyData == (Keys.Control | Keys.A)) { MessageBox.Show("Ctr+A"); } return base.ProcessDialogKey(keyData); } //从外部配置文件获取用户自定义组合键:比如XML,以ProcessCmdKey为例 protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { //获取快捷键列表 string shortCutKeys = XMLHelper.XmlRead2(XmlPath, "ShortCutKeys"); //假设键与键之间用+号连接 string[] keyStrs = shortCutKeys.Split('+'); Keys combineKey = Keys.None; KeysConverter kc = new KeysConverter(); foreach (string key in keyStrs) combineKey |= (Keys)kc.ConvertFromString(key.Trim()); if (keyData == combineKey) //do something return base.ProcessCmdKey(ref msg, keyData); }