品牌网网站建设公司,大连装修公司前十名,电商小程序名字大全,舟山论坛网本文经原作者授权以原创方式二次分享#xff0c;欢迎转载、分享。原文作者#xff1a;唐宋元明清原文地址#xff1a;https://www.cnblogs.com/kybs0/p/12558056.htmlC# 禁用 全局快捷键给软件添加快捷键时#xff0c;经常遇到其它软件或者系统已设置的快捷键#xff0c;导… 本文经原作者授权以原创方式二次分享欢迎转载、分享。原文作者唐宋元明清原文地址https://www.cnblogs.com/kybs0/p/12558056.html C# 禁用 全局快捷键给软件添加快捷键时经常遇到其它软件或者系统已设置的快捷键导致功能冲突。HotKey函数下面介绍一个user32.dll的RegisterHotKey以及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 namemodifiers/param/// param namekey/parampublic void Register(int modifiers, Keys key){Register(IntPtr.Zero, modifiers, key);}/// summary/// 注册快捷键/// /summary/// param namehWnd/param/// param namemodifiers/param/// param namekey/param/// param namecallBack/parampublic 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 namehWnd/param/// param namemodifiers/param/// param namekey/parampublic 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 namemodifiers/param/// param namekey/parampublic 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 namehWnd/parampublic 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 namemsg/param/// param namewParam消息Id/parampublic 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 ListHotkeyRegisterRecord _hotkeyRegisterRecords new ListHotkeyRegisterRecord();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是窗口响应时快捷键值在winform和WPF窗口消息函数中都是有的。另组合快捷键内部枚举类HotkeyModifiers枚举值来自官网文档WM_HOTKEY message;无感知禁用全局快捷键比如禁用CtrlAlt1、CtrlAlt2、CtrlAlt3、CtrlAlt4Windows桌面图标大小的调节快捷键;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程序集下如果是WPF的Key可以使用KeyInterop将Wpf键值类型转换为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) {//这里注册了CtrlAlt1 快捷键_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?redirectedfromMSDN[2]UnregisterHotKey function : https://docs.microsoft.com/zh-cn/windows/win32/api/winuser/nf-winuser-unregisterhotkey[3]源码下载: https://github.com/Kybs0/DiableGlobalShortcuts