在WPF中如何注册热键
不说废话,直接看代码吧,其关键就是 System.Windows.Interop.HwndSource类。
第一步:注册热键,需要使用API函数,具体的参照网上的其它文章。唯一需要注意的是需要使用KeyInterop.VirtualKeyFromKey函数将
WPF的Key枚举转化为API函数可以使用的VirtualKeyCode :
注册热键API函数定义
/// <summary>
/// 注册热键处理函数
/// </summary>
/// <param name="hWnd">用于处理热键消息的窗体句柄</param>
/// <param name="id">热键的编号</param>
/// <param name="controlKey">控制键</param>
/// <param name="virtualKey">热键的虚键编码</param>
/// <returns>
/// <c>true</c>:注册成功<br/>
/// <c>false</c>:注册失败
/// </returns>
/// <remarks></remarks>
/// <history>
/// [ZengE] 2009-7-8 22:28 创建
/// </history>
[System.Runtime.InteropServices.DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint controlKey, uint virtualKey);
/// <summary>
/// 注销指定的热键
/// </summary>
/// <param name="hWnd">用于处理热键消息的窗体句柄</param>
/// <param name="id">要注销的热键编号</param>
/// <returns>
/// <c>true</c>:注销成功<br/>
/// <c>false</c>:注销失败
/// </returns>
/// <remarks></remarks>
/// <history>
/// [ZengE] 2009-7-8 22:30 创建
/// </history>
[System.Runtime.InteropServices.DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
/// <summary>
/// 注册热键处理函数
/// </summary>
/// <param name="hWnd">用于处理热键消息的窗体句柄</param>
/// <param name="id">热键的编号</param>
/// <param name="controlKey">控制键</param>
/// <param name="virtualKey">热键的虚键编码</param>
/// <returns>
/// <c>true</c>:注册成功<br/>
/// <c>false</c>:注册失败
/// </returns>
/// <remarks></remarks>
/// <history>
/// [ZengE] 2009-7-8 22:28 创建
/// </history>
[System.Runtime.InteropServices.DllImport("user32")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint controlKey, uint virtualKey);
/// <summary>
/// 注销指定的热键
/// </summary>
/// <param name="hWnd">用于处理热键消息的窗体句柄</param>
/// <param name="id">要注销的热键编号</param>
/// <returns>
/// <c>true</c>:注销成功<br/>
/// <c>false</c>:注销失败
/// </returns>
/// <remarks></remarks>
/// <history>
/// [ZengE] 2009-7-8 22:30 创建
/// </history>
[System.Runtime.InteropServices.DllImport("user32")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
第二步:注册热键处理函数,主要的难点就在这里,在.NET的WinForm程序中一般使用重写Form的WinProc方法或者注册IMessageFilter来实现的,
但是在WPF中以上两种方式都无法获得热键消息的处理机会,除非是在WPF程序中隐藏一个常规的Form(最开始就是这么弄得,后来觉得实在是太丑陋了)
注册热键处理函数
1 /**//// <summary>
2 /// 安装热键处理挂钩
3 /// </summary>
4 /// <param name="window">The window.</param>
5 /// <returns>
6 /// <c>true</c>:安装成功<br/>
7 /// <c>false</c>:安装失败
8 /// </returns>
9 /// <value>消息源</value>
10 /// <remarks></remarks>
11 /// <history>
12 /// [ZengE] 2009-7-8 23:57 创建
13 /// </history>
14 public static bool InstallHotKeyHook( Window window )
15 {
16 //判断组件是否有效
17 if ( null == window )
18 {
19 //如果无效,则直接返回
20 return false;
21 }
22
23 //获得窗体的句柄
24 System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper( window );
25
26 //判断窗体句柄是否有效
27 if ( IntPtr.Zero == helper.Handle )
28 {
29 //如果句柄无效,则直接返回
30 return false;
31 }
32
33 //获得消息源
34 System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd( helper.Handle );
35
36 //判断消息源是否有效
37 if ( null == source )
38 {
39 //如果消息源无效,则直接返回
40 return false;
41 }
42
43 //挂接事件
44 source.AddHook( HotKeyHook );
45
46 //返回安装成功
47 return true;
48 }
49
50 /**//// <summary>
51 /// 热键处理过程
52 /// </summary>
53 /// <param name="hwnd">触发消息的窗口句柄</param>
54 /// <param name="msg">要被处理的消息编号</param>
55 /// <param name="wParam">消息参数</param>
56 /// <param name="lParam">消息参数</param>
57 /// <param name="handled">消息是否被处理</param>
58 /// <returns></returns>
59 /// <remarks></remarks>
60 /// <history>
61 /// [ZengE] 2009-7-8 23:54 创建
62 /// </history>
63 private static IntPtr HotKeyHook( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
64 {
65 //判断是否为热键消息
66 if ( msg == WM_HOTKEY )
67 {
68
69 }
70
71 //返回
72 return IntPtr.Zero;
73 }
74
75 /**//// <summary>
76 /// 热键消息编号
77 /// </summary>
78 private const int WM_HOTKEY = 0x0312;
1 /**//// <summary>
2 /// 安装热键处理挂钩
3 /// </summary>
4 /// <param name="window">The window.</param>
5 /// <returns>
6 /// <c>true</c>:安装成功<br/>
7 /// <c>false</c>:安装失败
8 /// </returns>
9 /// <value>消息源</value>
10 /// <remarks></remarks>
11 /// <history>
12 /// [ZengE] 2009-7-8 23:57 创建
13 /// </history>
14 public static bool InstallHotKeyHook( Window window )
15 {
16 //判断组件是否有效
17 if ( null == window )
18 {
19 //如果无效,则直接返回
20 return false;
21 }
22
23 //获得窗体的句柄
24 System.Windows.Interop.WindowInteropHelper helper = new System.Windows.Interop.WindowInteropHelper( window );
25
26 //判断窗体句柄是否有效
27 if ( IntPtr.Zero == helper.Handle )
28 {
29 //如果句柄无效,则直接返回
30 return false;
31 }
32
33 //获得消息源
34 System.Windows.Interop.HwndSource source = System.Windows.Interop.HwndSource.FromHwnd( helper.Handle );
35
36 //判断消息源是否有效
37 if ( null == source )
38 {
39 //如果消息源无效,则直接返回
40 return false;
41 }
42
43 //挂接事件
44 source.AddHook( HotKeyHook );
45
46 //返回安装成功
47 return true;
48 }
49
50 /**//// <summary>
51 /// 热键处理过程
52 /// </summary>
53 /// <param name="hwnd">触发消息的窗口句柄</param>
54 /// <param name="msg">要被处理的消息编号</param>
55 /// <param name="wParam">消息参数</param>
56 /// <param name="lParam">消息参数</param>
57 /// <param name="handled">消息是否被处理</param>
58 /// <returns></returns>
59 /// <remarks></remarks>
60 /// <history>
61 /// [ZengE] 2009-7-8 23:54 创建
62 /// </history>
63 private static IntPtr HotKeyHook( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
64 {
65 //判断是否为热键消息
66 if ( msg == WM_HOTKEY )
67 {
68
69 }
70
71 //返回
72 return IntPtr.Zero;
73 }
74
75 /**//// <summary>
76 /// 热键消息编号
77 /// </summary>
78 private const int WM_HOTKEY = 0x0312;
以上代码在Windows2008下测试通过。