本文经原作者授权以原创方式二次分享,欢迎转载、分享。

原文作者:唐宋元明清

原文地址:https://www.cnblogs.com/kybs0/p/12558056.html

C# 禁用 全局快捷键
给软件添加快捷键时,经常遇到其它软件或者系统已设置的快捷键,导致功能冲突。

HotKey函数

  • 下面介绍一个user32.dllRegisterHotKey以及UnregisterHotKey热键处理的函数;

    • 注册热键 RegisterHotKey function [1];

BOOL RegisterHotKey(HWND hWnd, //响应热键的窗口句柄,如果为空,则注册到调用线程上Int id, //热键的唯一标识UINT fsModifiers, //热键的辅助按键UINT vk //热键的键值
);
  • 解除注册热键UnregisterHotKey function [2];

BOOL WINAPI UnregisterHotKey( HWND hWnd,//热键注册的窗口 int  id//要解除注册的热键ID
);

添加热键注册和注销函数

Register方法 -  注册user32.dll函数RegisterHotKey以禁用全局键,并在缓存内添加禁用记录;

ProcessHotKey方法 - 外界全局键调用时,调用回调函数;

public class HotKeys{#region 注册快捷键/// <summary>/// 注册快捷键/// </summary>/// <param name="modifiers"></param>/// <param name="key"></param>public void Register(int modifiers, Keys key){Register(IntPtr.Zero, modifiers, key);}/// <summary>/// 注册快捷键/// </summary>/// <param name="hWnd"></param>/// <param name="modifiers"></param>/// <param name="key"></param>/// <param name="callBack"></param>public void Register(IntPtr hWnd, int modifiers, Keys key, HotKeyCallBackHanlder callBack = null){var registerRecord = _hotkeyRegisterRecords.FirstOrDefault(i => i.IntPtr == hWnd && i.Modifiers == modifiers && i.Key == key);if (registerRecord != null){UnregisterHotKey(hWnd, registerRecord.Id);_hotkeyRegisterRecords.Remove(registerRecord);}int id = registerId++;if (!RegisterHotKey(hWnd, id, modifiers, key))throw new Exception("注册失败!");_hotkeyRegisterRecords.Add(new HotkeyRegisterRecord(){Id = id,IntPtr = hWnd,Modifiers = modifiers,Key = key,CallBack = callBack});}#endregion#region 注销快捷键/// <summary>/// 注销快捷键/// </summary>/// <param name="hWnd"></param>/// <param name="modifiers"></param>/// <param name="key"></param>public void UnRegister(IntPtr hWnd, int modifiers, Keys key){var registerRecord = _hotkeyRegisterRecords.FirstOrDefault(i => i.IntPtr == hWnd && i.Modifiers == modifiers && i.Key == key);if (registerRecord != null){UnregisterHotKey(hWnd, registerRecord.Id);_hotkeyRegisterRecords.Remove(registerRecord);}}/// <summary>/// 注销快捷键/// </summary>/// <param name="modifiers"></param>/// <param name="key"></param>public void UnRegister(int modifiers, Keys key){var registerRecord = _hotkeyRegisterRecords.FirstOrDefault(i => i.IntPtr == IntPtr.Zero && i.Modifiers == modifiers && i.Key == key);if (registerRecord != null){UnregisterHotKey(IntPtr.Zero, registerRecord.Id);_hotkeyRegisterRecords.Remove(registerRecord);}}/// <summary>/// 注销快捷键/// </summary>/// <param name="hWnd"></param>public void UnRegister(IntPtr hWnd){var registerRecords = _hotkeyRegisterRecords.Where(i => i.IntPtr == hWnd);//注销所有foreach (var registerRecord in registerRecords){UnregisterHotKey(hWnd, registerRecord.Id);_hotkeyRegisterRecords.Remove(registerRecord);}}#endregion#region 快捷键消息处理// 快捷键消息处理public void ProcessHotKey(Message message){ProcessHotKey(message.Msg, message.WParam);}/// <summary>/// 快捷键消息处理/// </summary>/// <param name="msg"></param>/// <param name="wParam">消息Id</param>public void ProcessHotKey(int msg, IntPtr wParam){if (msg == 0x312){int id = wParam.ToInt32();var registerRecord = _hotkeyRegisterRecords.FirstOrDefault(i => i.Id == id);registerRecord?.CallBack?.Invoke();}}#endregion#region MyRegion//引入系统API[DllImport("user32.dll")]static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);[DllImport("user32.dll")]static extern bool UnregisterHotKey(IntPtr hWnd, int id);//标识-区分不同的快捷键int registerId = 10;//添加key值注册字典,后续调用时有回调处理函数private readonly List<HotkeyRegisterRecord> _hotkeyRegisterRecords = new List<HotkeyRegisterRecord>();public delegate void HotKeyCallBackHanlder();#endregion}public class HotkeyRegisterRecord{public IntPtr IntPtr { get; set; }public int Modifiers { get; set; }public Keys Key { get; set; }public int Id { get; set; }public HotKeys.HotKeyCallBackHanlder CallBack { get; set; }}//组合控制键public enum HotkeyModifiers{Alt = 1,Control = 2,Shift = 4,Win = 8}
  • 在上方的HotKeys类中,注册方法Register提供了一个回调函数,后续监听到外界全局键时,可以通知回调函数处理。

  • 参数WParam,是窗口响应时快捷键值,在winformWPF窗口消息函数中都是有的。

  • 另,组合快捷键内部枚举类HotkeyModifiers,枚举值来自官网文档WM_HOTKEY message;

无感知禁用全局快捷键

比如:

  • 禁用Ctrl+Alt+1、Ctrl+Alt+2、Ctrl+Alt+3、Ctrl+Alt+4(Windows桌面图标大小的调节快捷键);

HotKeys hotKeys = new HotKeys();hotKeys.Register((int)HotkeyModifiers.Control, Keys.N);hotKeys.Register((int)HotkeyModifiers.Control + (int)HotkeyModifiers.Alt, Keys.D1);hotKeys.Register((int)HotkeyModifiers.Control + (int)HotkeyModifiers.Alt, Keys.D2);hotKeys.Register((int)HotkeyModifiers.Control + (int)HotkeyModifiers.Alt, Keys.D3);hotKeys.Register((int)HotkeyModifiers.Control + (int)HotkeyModifiers.Alt, Keys.D4);

注:

  • 窗口句柄参数,如果提供空的话,则注册到调用线程上。

  • Keys类型在system.windows.Forms程序集下,如果是WPFKey,可以使用KeyInteropWpf键值类型转换为Winform键值再调用此函数。

无感知禁用全局快捷键后回调

如果禁用全局快捷键的同时,外界触发快捷键时需要此程序回调处理,可以添加窗口消息处理:

1) 新建一个类HotKeyHandleWindow,继承自Window;

  • 窗口样式 - 高宽为0,窗口样式None;

  • 添加热键注册的调用;

  • 添加WndProc,处理窗口消息;

public class HotKeyHandleWindow : Window{private readonly HotKeys _hotKeys = new HotKeys();public HotKeyHandleWindow(){WindowStyle = WindowStyle.None;Width = 0;Height = 0;Loaded += (s, e) =>{//这里注册了Ctrl+Alt+1 快捷键_hotKeys.Register(new WindowInteropHelper(this).Handle,(int)HotkeyModifiers.Control + (int)HotkeyModifiers.Alt, Keys.D1, CallBack);};}protected override void OnSourceInitialized(EventArgs e){base.OnSourceInitialized(e);var hwndSource = PresentationSource.FromVisual(this) as HwndSource;hwndSource?.AddHook(new HwndSourceHook(WndProc));}public IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){//窗口消息处理函数_hotKeys.ProcessHotKey(msg, wParam);return hwnd;}//按下快捷键时被调用的方法public void CallBack(){}}

2)调用窗口类;

var hotKeyHandleWindow = new HotKeyHandleWindow();
hotKeyHandleWindow.Show();
hotKeyHandleWindow.Hide();

以上有回调响应,但是也是无感知的

源码下载[3]

参考资料

[1]

RegisterHotKey function : https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-registerhotkey?redirectedfrom=MSDN

[2]

UnregisterHotKey function : https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-unregisterhotkey

[3]

源码下载: https://github.com/Kybs0/DiableGlobalShortcuts

技术群:添加小编微信并备注进群

小编微信:mm1552923

公众号:dotNet编程大全

C# 禁用 全局快捷键相关推荐

  1. WPF设置全局快捷键

    网上的几分文档都似乎有点儿问题.也很可能是我自己的问题.下面是我的解决方案 第一步 引入到Winows API 偷懒直接写在类里 1: [DllImport("user32.dll" ...

  2. .net winfrom 定义全局快捷键!

    public class Test  Dim myTimeNow As String     '全局快捷键     Public Const WM_HOTKEY = &H312     Pub ...

  3. win10 全局快捷键设置启动程序

    原文链接: win10 全局快捷键设置启动程序 上一篇: Python 安装anaconda 和conda 的简单使用 下一篇: surface pro win10 重装系统并解决屏幕亮度闪烁和降频的 ...

  4. C#注册和注销全局快捷键

    如何在C#代码中实现全局快捷键 C#.NET没有提供现成的API,我们通过引用系统的API进行注册 1.首先,创建一个快捷键操作的类,可以完成注册,注销的操作,具体说明看注释 public class ...

  5. 一个window下的简单的全局快捷键向指定的进程发送的c代码与exe程序下载(二)

    c代码:一个window下的简单的全局快捷键向指定的进程发送的c代码与exe程序下载 -----------------这是文件 hotkey.zip base64后的字符,复制代码时不要复制我(共2 ...

  6. 禁用键盘快捷键_如何在Windows中使用键盘快捷键临时禁用键盘

    禁用键盘快捷键 If you've got a pet or small child, you know that an unguarded keyboard can spell disaster-o ...

  7. windwos设置GifCam录屏全局快捷键

    对于自己写的或者一些实用的小工具,可以将其保存到C:\Windows,这样我们可以通过命令行的方式打开,当然其实本质只需要在系统的PATH路径下都可以在命令行打开. 下面gifcam是一个第三方的第三 ...

  8. 台式机win10关闭fn热键_Win10系统怎么禁用f1-f12快捷键

    Win10系统怎么禁用f1-f12快捷键?很多小伙伴把电脑操作系统从win7升级到win10后,会有很多的使用技巧等待我们去发现,今天我们要聊的是win10快捷键的一些知识,本文我们来看看Win10系 ...

  9. windows 设置全局快捷键;

    简单设置全局快捷键: 组合键win+R 打开控制面板,小图表显示下 点击 管理工具项, 将自己想要谁知快捷键的程序的快捷方式放进去,(需要确认管理员权限) 如图,第一个即为 lz添加的 右击选择属性 ...

最新文章

  1. ECshop--搜索模块细究
  2. Hibernate中的HQL的基本常用小例子,单表查询与多表查询
  3. Java注解原来如此通俗易懂
  4. boost::container模块实现vector选项
  5. Android Studio 中快速提取方法
  6. 同步fifo的串并_同步FIFO设计Spec(示例代码)
  7. mysql常见数据库设计_常见数据库设计
  8. 周鸿祎:物联网时代的三大威胁
  9. php com(),php|luosimao.com文档中心
  10. PostgreSQL代码分析,查询优化部分,canonicalize_qual
  11. java并发包作者lee_Java的一些并发包
  12. mysql中union,左连接,右连接,与内连接
  13. WingPro 8 for Mac(专业Python IDE开发工具)
  14. nginx 禁止访问配置,指定URL地址指定IP允许访问
  15. JN5169 基于 JN-AN-1217 组网点灯
  16. 济南月薪一万是什么水平?
  17. 发货单分期发货分期收款
  18. html messagebox确定取消,Element MessageBox弹框的详细使用
  19. feign.RetryableException: connect timed out executing xxxxxx
  20. H5营销海报如何制作,在线制作平台分享

热门文章

  1. 360度不停旋转动画-转圈圈
  2. 穿越大海道(敦煌、吐鲁番)
  3. 大数据开发步骤和流程
  4. 计算机组成原理学习笔记:主机各个硬件部件和工作过程
  5. Vue实战项目:医院挂号系统
  6. 2021-05-27 WMS系统中的二维码技术应用
  7. 前端大神是如何一步步学习进阶的?
  8. 头条号发视频一直重复,求消重方法 今日头条发视频重复,怎么消重去重伪原创...
  9. mysql的replace_mySQL中replace的用法
  10. 武汉科技大学计算机学院研究生复试,武汉科技大学计算机考研复试科目