| 特性 | Mnemonics (Alt+字母) | Accelerators / KeyDown (Ctrl+...) |
|---|---|---|
| 觸發方式 | 按住 Alt 顯示底線,再按字母 | 直接按組合鍵 (Ctrl+S, F1 等) |
| 適用控制項 | Button, Label, MenuItem, CheckBox, RadioButton 等有 Text 屬性者 | Form、任意控制項 (需 KeyPreview) |
| 設定方式 | Text 屬性加 & 前綴 | 程式碼處理 KeyDown/KeyUp 事件 |
| 焦點需求 | 控制項需可獲焦點或其容器聚焦 | Form 需 KeyPreview=true 攔截所有按鍵 |
| 國際化 | 底線位置需隨語言調整 | 鍵碼固定,較易本地化 |
在控制項 Text 屬性中,在目標字母前加 &:
&Open → 顯示 Open,Alt+O 觸發Save &As → 顯示 Save As,Alt+A 觸發&& → 顯示單一 & 字元(轉義)
// 設計師中:Label 的 Text 設為 "&User Name:"
// Label 的 UseMnemonic = true (預設)
// 設定 Label 的焦點目標
labelUserName.UseMnemonic = true;
// 在屬性視窗或程式碼設定
labelUserName.Focus(); // 不會聚焦 Label
// 正確做法:設定 Label 的 AssociatedControl (VS 設計師自動處理)
// 或程式碼:
this.labelUserName.Click += (s, e) => this.textBoxUserName.Focus();
// 更佳:使用 TableLayoutPanel + Label + TextBox,VS 設計師自動關聯
// MenuStrip 設計師中直接在 Text 設定 "&File"、"E&xit"
// 執行時按 Alt 顯示底線,按 F 打開 File 選單,按 X 執行 Exit
public partial class MainForm : Form {
public MainForm() {
InitializeComponent();
// 關鍵:讓 Form 先於子控制項收到按鍵事件
this.KeyPreview = true;
this.KeyDown += MainForm_KeyDown;
}
private void MainForm_KeyDown(object sender, KeyEventArgs e) {
// Ctrl+S 儲存
if (e.Control && e.KeyCode == Keys.S) {
e.Handled = true; // 標記已處理,阻止進一步傳遞
SaveDocument();
}
// Ctrl+Shift+S 另存新檔
else if (e.Control && e.Shift && e.KeyCode == Keys.S) {
e.Handled = true;
SaveDocumentAs();
}
// F5 重新整理
else if (e.KeyCode == Keys.F5) {
e.Handled = true;
RefreshData();
}
// Esc 關閉對話框/取消
else if (e.KeyCode == Keys.Escape) {
e.Handled = true;
this.Close(); // 或 CancelOperation()
}
}
}
using System.Collections.Generic;
using System.Windows.Forms;
public class ShortcutManager {
private readonly Form _form;
private readonly Dictionary _shortcuts = new Dictionary();
public ShortcutManager(Form form) {
_form = form;
_form.KeyPreview = true;
_form.KeyDown += OnKeyDown;
}
public void Register(Keys keyData, Action action) {
_shortcuts[keyData] = action;
}
public void Unregister(Keys keyData) {
_shortcuts.Remove(keyData);
}
private void OnKeyDown(object sender, KeyEventArgs e) {
Keys keyData = e.KeyCode | (e.Control ? Keys.Control : Keys.None)
| (e.Shift ? Keys.Shift : Keys.None)
| (e.Alt ? Keys.Alt : Keys.None);
if (_shortcuts.TryGetValue(keyData, out var action)) {
e.Handled = true;
e.SuppressKeyPress = true; // 防止輸入控制項接收字元
action.Invoke();
}
}
}
// 使用範例
public partial class MainForm : Form {
private ShortcutManager _shortcuts;
public MainForm() {
InitializeComponent();
_shortcuts = new ShortcutManager(this);
_shortcuts.Register(Keys.Control | Keys.S, SaveDocument);
_shortcuts.Register(Keys.Control | Keys.Shift | Keys.S, SaveDocumentAs);
_shortcuts.Register(Keys.F5, RefreshData);
_shortcuts.Register(Keys.Escape, () => Close());
}
}
設計師中選取 ToolStripMenuItem,設定 ShortcutKeys 屬性 (如 Ctrl+S),ShowShortcut 顯示於選單旁。自動處理 KeyPreview、事件路由。
| 問題 | 原因 | 解決 |
|---|---|---|
| Alt+字母無反應 | 控制項不可聚焦、UseMnemonic=false、衝突 | 檢查 TabStop、UseMnemonic、確保無重複字母 |
| KeyDown 不觸發 | KeyPreview=false、子控制項攔截 (如 TextBox 捕獲 Enter) | 設 KeyPreview=true、在控制項 KeyDown 也處理或 SuppressKeyPress |
| 快捷鍵與輸入法衝突 | 中文輸入法佔用 Ctrl+Space 等 | 避開常用輸入法熱鍵、提供替代方案 |
| 多表單快捷鍵衝突 | 子表單也設 KeyPreview | 只有主表單設 KeyPreview,或用 IMessageFilter 全域攔截 |
WinForms 快捷鍵三大支柱:Mnemonics (Alt+字母) 針對選單/按鈕標籤、KeyPreview + KeyDown 針對全域功能鍵、MenuStrip.ShortcutKeys 針對選單項目。掌握三者適用場景,配合 e.Handled、e.SuppressKeyPress 控制事件流向,即可建立專業級鍵盤操作體驗。