快捷鍵輔助類
class HotKey
{
/// <summary>
/// 如果函數執行成功,返回值不為0。
/// 如果函數執行失敗,返回值為0。要得到擴展錯誤信息,調用GetLastError。.NET方法:Marshal.GetLastWin32Error()
/// </summary>
/// <param name="hWnd">要定義熱鍵的窗口的句柄</param>
/// <param name="id">定義熱鍵ID(不能與其它ID重復) </param>
/// <param name="fsModifiers">標識熱鍵是否在按Alt、Ctrl、Shift、Windows等鍵時才會生效</param>
/// <param name="vk">定義熱鍵的內容,WinForm中可以使用Keys枚舉轉換,
/// WPF中Key枚舉是不正確的,應該使用System.Windows.Forms.Keys枚舉,或者自定義正確的枚舉或int常量</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool RegisterHotKey(
IntPtr hWnd,
int id,
KeyModifiers fsModifiers,
int vk
);
/// <summary>
/// 取消注冊熱鍵
/// </summary>
/// <param name="hWnd">要取消熱鍵的窗口的句柄</param>
/// <param name="id">要取消熱鍵的ID</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
public static extern bool UnregisterHotKey(
IntPtr hWnd,
int id
);
/// <summary>
/// 向全局原子表添加一個字符串,并返回這個字符串的唯一標識符,成功則返回值為新創建的原子ID,失敗返回0
/// </summary>
/// <param name="lpString"></param>
/// <returns></returns>
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalAddAtom(string lpString);
[DllImport("kernel32", SetLastError = true)]
public static extern short GlobalDeleteAtom(short nAtom);
/// <summary>
/// 定義了輔助鍵的名稱(將數字轉變為字符以便于記憶,也可去除此枚舉而直接使用數值)
/// </summary>
[Flags()]
public enum KeyModifiers
{
None = 0,
Alt = 1,
Ctrl = 2,
Shift = 4,
WindowsKey = 8
}
/// <summary>
/// 熱鍵的對應的消息ID
/// </summary>
public const int WM_HOTKEY = 0x312;
}
WinForm方法:
在中在onload中注冊事件,然后重載WndProc,對返回的消息進行處理
int alts,altd;
protected override void OnLoad(EventArgs e)
{
alts= HotKey.GlobalAddAtom("Alt-S");
altd= HotKey.GlobalAddAtom("Alt-D");
HotKey.RegisterHotKey(this.Handle, alts, HotKey.KeyModifiers.Alt, (int)Keys.S);
HotKey.RegisterHotKey(this.Handle, altd, HotKey.KeyModifiers.Alt, (int)Keys.D);
}
protected override void WndProc(ref Message m)// 監視Windows消息
{
switch (m.Msg)
{
case HotKey.WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
base.WndProc(ref m); //將系統消息傳遞自父類的WndProc
}
private void ProcessHotkey(Message m) //按下設定的鍵時調用該函數
{
IntPtr id = m.WParam;//IntPtr用于表示指針或句柄的平臺特定類型
int sid=id.ToInt32();
if(sid==alts)
{
MessageBox.Show("按下Alt+S");
}
else if(sid==altd)
{
MessageBox.Show("按下Alt+D");
}
}
也可以在Application使用AddMessageFilter添加處理來代替重載WndProc,可以使form自身實現IMessageFilter接口,
注冊方法
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new Form1();
Application.AddMessageFilter(form);
Application.Run(form);
}
實現接口
bool IMessageFilter.PreFilterMessage(ref Message m)
{
const int WM_HOTKEY = 0x0312;//如果m.Msg的值為0x0312那么表示用戶按下了熱鍵
switch (m.Msg)
{
case WM_HOTKEY:
ProcessHotkey(m);//按下熱鍵時調用ProcessHotkey()函數
break;
}
//如果篩選消息并禁止消息被調度,則為 true;如果允許消息繼續到達下一個篩選器或控件,則為 false
return false ;
}
如果同時在AddMessageFilter和WndProc處理消息,順序是先IMessageFilter,然后調用WndProc
WPF方法:
WPF中的注冊方法和Winform一樣,只是WPF的Key枚舉的值不能正確監聽,要引用System.Windows.Forms.Keys或者自己定義才可以正確注冊,窗口句柄也需要借助WindowInteropHelper來得到,處理函數的加入也和Winform不同,需要HwndSource來添加處理函數。
注冊及處理方法
int alts, altd;
private void Window_Loaded(object sender, RoutedEventArgs e)
{
HwndSource hWndSource;
WindowInteropHelper wih = new WindowInteropHelper(this);
hWndSource = HwndSource.FromHwnd(wih.Handle);
//添加處理程序
hWndSource.AddHook(MainWindowProc);
alts = HotKey.GlobalAddAtom("Alt-S");
altd = HotKey.GlobalAddAtom("Alt-D");
HotKey.RegisterHotKey(wih.Handle, alts, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.S);
HotKey.RegisterHotKey(wih.Handle, altd, HotKey.KeyModifiers.Alt, (int)System.Windows.Forms.Keys.D);
}
private IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case HotKey.WM_HOTKEY:
{
int sid = wParam.ToInt32();
if (sid == alts)
{
MessageBox.Show("按下Alt+S");
}
else if (sid == altd)
{
MessageBox.Show("按下Alt+D");
}
handled = true;
break;
}
}
return IntPtr.Zero;
}
注意:
如果注冊快捷鍵,RegisterHotKey中的fsModifiers參數為0,即None選項,一些安全軟件會警報,可能因為這樣就可以全局監聽鍵盤而造成安全問題
代碼下載