• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

c#为程序添加全局热键的方法

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

在程序失去焦点或者在后台运行时,可以通过使用全局热键的方式,进行一些快捷的操作,如QQ默认操作中ctrl+alt+A调出截图功能。

在Windows中实现热键功能需要使用win32的Api函数RegisterHotKey和UnregisterHotKey。

示例Demo(含代码)

实现代码:

一、注册热键:

 

 1     public class HotKey
 2     {
 3         //============= 1、声明注册热键的方法 ==================
 4         [DllImport("user32.dll", EntryPoint = "RegisterHotKey")]
 5         private static extern int RegisterHotKey(IntPtr hWnd, int nID, int nModifiers, int nVK);
 6 
 7         [DllImport("user32.dll", EntryPoint = "RegisterHotKey")]
 8         private static extern int RegisterHotKey(IntPtr hWnd, int nID, int nModifiers, Keys VK);
 9 
10         [DllImport("user32.dll", EntryPoint = "UnregisterHotKey")]
11         private static extern int UnregisterHotKey(IntPtr hWnd, int nID);
12 
13         //============= 2、声明组合键常量 ========================
14         public const int MOD_NONE = 0;
15         public const int MOD_ALT = 1;
16         public const int MOD_CTRL = 2;
17         public const int MOD_SHIFT = 4;
18 
19         public enum MOD
20         {
21             MOD_NONE = 0,
22             MOD_ALT = 1,
23             MOD_CTRL = 2,
24             MOD_SHIFT = 4,
25             MOD_WIN = 8
26         }
27 
28         //============= 3、实现注册热键的方法 ====================
29 
30         /// <summary>
31         /// 注册热键
32         /// </summary>
33         /// <param name="hWnd">窗口句柄</param>
34         /// <param name="nID">热键标识</param>
35         /// <param name="modKey">组合键</param>
36         /// <param name="nVK">热键</param>
37         /// <returns></returns>
38         public static bool RegHotKey(IntPtr hWnd, int nID, int modKey, int nVK)
39         {
40             //========== 3.1、先释放该窗口句柄下具有相同标识的热键 =============
41             UnregisterHotKey(hWnd, nID);
42 
43             //========== 3.2、注册热键 =========================================
44             int nResult = RegisterHotKey(hWnd, nID, modKey, nVK);
45 
46             //========== 3.3、返回注册结果 =====================================
47             return nResult != 0 ? true : false;
48         }
注册热键

 

 

二、在调用热键的窗口程序中,重写WndProc方法响应热键:

 

 

 1         private const int nHotKeyID = 0xabcd;           //热键标识
 2         /// <summary>
 3         /// 重写WndProc响应热键方法
 4         /// </summary>
 5         /// <param name="m"></param>
 6         protected override void WndProc(ref Message m)
 7         {
 8             switch (m.WParam.ToInt32())
 9             {
10                 case nHotKeyID:
11                     Method();       //热键调用的方法
12                     break;
13             }
14 
15             base.WndProc(ref m);
16         }
响应热键

 

全局热键的注册工作完成,还有一些需要注意的方面:

1、关于定义热键的标识符,引用程序必须定义一个0X0000-0xBFFF范围的值;

2、经测试,F12键无法进行注册,有可能是系统占用。(如有误,还请路过的高手指教);

附上示例Demo(含代码)

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
c#专业的UVC摄像头深控类库-SharpCamera介绍发布时间:2022-07-13
下一篇:
C#:判断两个图片是否为同一张图片发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap