在 Windows 桌面應用程式中,註冊全域熱鍵(Global Hotkey)能讓使用者在任何視窗焦點下,透過特定組合鍵觸發程式功能(如截圖、快速啟動、剪貼板操作等)。C# 本身不提供直接 API,需透過 P/Invoke 呼叫 user32.dll 的 RegisterHotKey 與 UnregisterHotKey,並覆寫 WndProc 處理 WM_HOTKEY 訊息。本文將完整示範從定義、註冊、訊息處理到資源釋放的標準流程。
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public static class NativeMethods
{
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, KeyModifiers fsModifiers, Keys vk);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
[Flags]
public enum KeyModifiers : uint
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8
}
private const int HOTKEY_ID = 100; // 唯一識別碼
private void Form1_Load(object sender, EventArgs e)
{
// 可選:清空剪貼板避免干擾
Clipboard.Clear();
// 註冊 F10 為熱鍵(無修飾鍵)
if (!NativeMethods.RegisterHotKey(this.Handle, HOTKEY_ID, KeyModifiers.None, Keys.F10))
{
MessageBox.Show("熱鍵註冊失敗,可能已被其他程式佔用。");
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
// 必須釋放熱鍵,否則系統保留佔用
NativeMethods.UnregisterHotKey(this.Handle, HOTKEY_ID);
}
protected override void WndProc(ref Message m)
{
const int WM_HOTKEY = 0x0312;
if (m.Msg == WM_HOTKEY)
{
// m.WParam 為熱鍵 ID,可區分多組熱鍵
if ((int)m.WParam == HOTKEY_ID)
{
ProcessHotkey();
}
}
base.WndProc(ref m);
}
private void ProcessHotkey()
{
// 這裡撰寫熱鍵觸發的核心邏輯
// 例如:顯示/隱藏主視窗、截圖、剪貼板操作等
this.Visible = !this.Visible;
if (this.Visible) this.Activate();
}
// 註冊 Ctrl+Alt+S
NativeMethods.RegisterHotKey(this.Handle, 101, KeyModifiers.Control | KeyModifiers.Alt, Keys.S);
// 註冊 Win+Q
NativeMethods.RegisterHotKey(this.Handle, 102, KeyModifiers.Windows, Keys.Q);
// WndProc 中判斷
if (m.Msg == WM_HOTKEY)
{
switch ((int)m.WParam)
{
case 100: ProcessHotkey_F10(); break;
case 101: ProcessHotkey_CtrlAltS(); break;
case 102: ProcessHotkey_WinQ(); break;
}
}
const int 或 enum 管理。RegisterHotKey 回傳 false 時,可呼叫 Marshal.GetLastWin32Error() 取得錯誤碼(1409 = 熱鍵已被註冊)。Clipboard.SetDataObject 並處理 ExternalException(剪貼板被佔用)。HwndSource.AddHook 掛鉤 WndProc,邏輯相同。掌握 RegisterHotKey / UnregisterHotKey 與 WndProc 訊息迴圈,即可在 C# 輕鬆實現全域熱鍵功能。記得在表單關閉時確實釋放熱鍵,並妥善處理多組熱鍵的 ID 管理與錯誤情況,即可打造專業級的桌面工具體驗。