參考自:http://www.csharpwin.com/csharpspace/3932r8132.shtml
一、C# button快捷鍵之第一種:Alt + *(按鈕快捷鍵)
在Button按鈕的Text屬性的文本名稱加(&鍵名)就可以了,如button1.Text = "保存(&S)"; 這樣只要按Alt+S就可以執行按鈕的單擊事件。
二、C# button快捷鍵之第二種:Ctrl+*及其他組合鍵
在WinForm中設置要使用組合鍵的窗體的KeyPreview(向窗體注冊鍵盤事件)屬性為True;然后使用窗體的KeyDown事件(在首次按下某個鍵時發生)或KeyPress事件或KeyUp事件
private void Form1_KeyDown(object sender, KeyEventArgs e) {
// if (Control.ModifierKeys == Keys.Control && e.KeyCode == Keys.S) if (e.KeyCode == Keys.S && e.Control) { button1.PerformClick(); //執行單擊button1的動作 } }
還有一個問題,當使用Ctrl + *快捷鍵時,對於焦點在可寫的控件(如TextBox)上時,可能會將* 鍵值同時輸入,則需要加另一句話將Handled設置為true,以取消 KeyDown 事件。
private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.S && e.Control) { e.Handled = true; //將Handled設置為true,指示已經處理過KeyDown事件 button1.PerformClick(); //執行單擊button1的動作 } }
三、C# button快捷鍵之第三種方法(不推薦使用該方式)
還是以button為例。給form添加一個contextMenuStrip1,將其邦定到form窗體的ContextMenuStrip屬性上。給contextMenuStrip1添加一個item,然后為它設置快捷鍵(在右鍵菜單項的ShortcutKeys屬性上設置快捷鍵),將它的Visible屬性設為false,並且將它的點擊事件設為button的點擊事件方法。這樣,C#button快捷鍵設置成功。
四、C# button快捷鍵之第四種方法:重寫ProcessCmdKey(ref Message msg, Keys keyData)方法
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { //處理要做的事, //不希望做其它做用,return true // 要做其它的 return base.ProcessCmdKey(ref msg, keyData); }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { // 按快捷鍵Ctrl+S執行按鈕的點擊事件方法 if (keyData == (Keys)Shortcut.CtrlS) { button1.PerformClick(); return true; } return base.ProcessCmdKey(ref msg, keyData); // 其他鍵按默認處理 }
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { // 實現按Enter鍵來實現Ctrl+S鍵的功能 if (keyData == Keys.Enter) { SendKeys.Send("^s"); return true; } // 按快捷鍵Ctrl+S執行按鈕的點擊事件方法 //if (keyData == (Keys.Control | Keys.S))
if (keyData == (Keys)Shortcut.CtrlS) { button1.PerformClick(); return true; } return base.ProcessCmdKey(ref msg, keyData); // 其他鍵按默認處理
}
SendKeys.Send(string keys),要發送的鍵字符串,可以查看MSDN:http://msdn.microsoft.com/zh-cn/library/system.windows.forms.sendkeys.send(v=VS.80).aspx
如果ProcessCmdKey(ref Message msg, Keys keyData)方法的效果不行,可以試試ProcessDialogKey(Keys keyData)方法
protected override bool ProcessDialogKey(Keys keyData) { if (keyData == Keys.Enter) { keyData = Keys.Tab; //ProcessTabKey(true); } return base.ProcessDialogKey(keyData); }
另:Form 重寫ProcessCmdKey: 解決enter同時Tab 的問題
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) { if (keyData == Keys.Enter && ((!(ActiveControl is System.Windows.Forms.TextBox) || !((System.Windows.Forms.TextBox)ActiveControl).AcceptsReturn))) { SendKeys.SendWait("{Tab}"); return true; } if (keyData == ( Keys.Enter | Keys.Shift)) { SendKeys.SendWait("+{Tab}"); return true; } return base.ProcessCmdKey(ref msg, keyData); }
五、C# button快捷鍵之第五種方法:使用系統API (來自:http://blog.sina.com.cn/s/blog_674a665801013hit.html)
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Runtime.InteropServices; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { MessageBox.Show("使用快捷鍵啟動按鈕"); } private void Form1_Load(object sender, EventArgs e) { //注冊熱鍵Ctrl+F12,這里的8879就是一個ID識別 RegisterHotKey(this.Handle, 8879, 2, Keys.F12); } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //用來取消注冊的熱鍵 UnregisterHotKey(this.Handle, 8879); } /// <summary> /// 注冊熱鍵 /// </summary> /// <param name="hWnd">為窗口句柄</param> /// <param name="id">注冊的熱鍵識別ID</param> /// <param name="control">組合鍵代碼 Alt的值為1,Ctrl的值為2,Shift的值為4,Shift+Alt組合鍵為5 /// Shift+Alt+Ctrl組合鍵為7,Windows鍵的值為8 /// </param> /// <param name="vk">按鍵枚舉</param> /// <returns></returns> [DllImport("user32")] public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint control, Keys vk); /// <summary> /// 取消注冊的熱鍵 /// </summary> /// <param name="hWnd">窗口句柄</param> /// <param name="id">注冊的熱鍵id</param> /// <returns></returns> [DllImport("user32")] public static extern bool UnregisterHotKey(IntPtr hWnd, int id); // 響應熱鍵 protected override void WndProc(ref Message m) { switch (m.Msg) { case 0x0312: //這個是window消息定義的注冊的熱鍵消息 if (m.WParam.ToString().Equals("8879")) //如果是注冊的那個熱鍵 { // 執行button按鈕 button1.PerformClick(); } break; } base.WndProc(ref m); } } }