当前位置: 首页 > news >正文

c# 快捷键模块

文章目录

      • 命名空间和类
      • 类成员
        • 静态成员
      • 静态方法
        • `GenerateHotkeyId`
        • `WndProc`
        • `GetWindowHandleAndSource`
        • `Register`
        • `Unregister`
      • 静态方法(外部调用)
        • `RegisterHotKey` 和 `UnRegisterHotKey`
      • 委托
        • `HotKeyCallbackHandler`
      • 枚举
        • `HotkeyModifiers`
    • 应用示例

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;namespace HotKeyModule
{public static class Hotkey{private static readonly Dictionary<int, HotKeyCallbackHandler> keymap = new Dictionary<int, HotKeyCallbackHandler>();private static int nextHotkeyId = 10;private static readonly object keymapLock = new object();private static int GenerateHotkeyId() => Interlocked.Increment(ref nextHotkeyId);// 窗口消息处理private static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled){const int WM_HOTKEY = 0x0312;if (msg == WM_HOTKEY){int hotkeyId = wParam.ToInt32();if (keymap.TryGetValue(hotkeyId, out var callback)){callback();handled = true;}}return IntPtr.Zero;}// 获取窗口句柄和HwndSourceprivate static (IntPtr hwnd, HwndSource hwndSource) GetWindowHandleAndSource(Window window){var hwnd = new WindowInteropHelper(window).Handle;var hwndSource = HwndSource.FromHwnd(hwnd) ?? throw new InvalidOperationException("Failed to get HwndSource.");return (hwnd, hwndSource);}// 注册热键public static void Register(Window window, HotkeyModifiers modifiers, Key key, HotKeyCallbackHandler callback){var (hwnd, hwndSource) = GetWindowHandleAndSource(window);hwndSource.AddHook(WndProc);int hotkeyId = GenerateHotkeyId();uint vk = (uint)KeyInterop.VirtualKeyFromKey(key);if (!RegisterHotKey(hwnd, hotkeyId, (uint)modifiers, vk)){throw new InvalidOperationException("Failed to register hotkey. Ensure the key combination is not already in use.");}lock (keymapLock){keymap[hotkeyId] = callback;}}// 注销热键public static void Unregister(Window window, HotKeyCallbackHandler callback){var (hwnd, _) = GetWindowHandleAndSource(window);lock (keymapLock){foreach (var kvp in keymap){if (kvp.Value == callback){UnRegisterHotKey(hwnd, kvp.Key);keymap.Remove(kvp.Key);break;}}}}[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);[DllImport("user32.dll")][return: MarshalAs(UnmanagedType.Bool)]private static extern bool UnRegisterHotKey(IntPtr hWnd, int id);}// 热键回调委托public delegate void HotKeyCallbackHandler();public enum HotkeyModifiers{ALT = 0x0001,CTRL = 0x0002,SHIFT = 0x0004,WIN = 0x0008,CTRL_ALT = CTRL|ALT,CTRL_SHIFT = CTRL | SHIFT,}
}

命名空间和类

代码定义在一个名为 HotKeyModule 的命名空间中,其中包含一个静态类 Hotkey,用于管理热键的注册和注销。

类成员

静态成员
  1. keymap:

    • 类型:Dictionary<int, HotKeyCallbackHandler>
    • 用途:存储热键ID和对应的回调函数。
  2. nextHotkeyId:

    • 类型:int
    • 用途:生成唯一的热键ID。
    • 初始值:10
  3. keymapLock:

    • 类型:object
    • 用途:用于同步访问 keymap,确保线程安全。

静态方法

GenerateHotkeyId
  • 用途:生成唯一的热键ID。
  • 实现:使用 Interlocked.Increment 方法原子性地增加 nextHotkeyId,并返回其新的值。
WndProc
  • 用途:处理窗口消息,特别是热键消息。
  • 参数:
    • hwnd:窗口句柄
    • msg:消息类型
    • wParam:消息参数
    • lParam:消息参数
    • handled:是否处理了消息
  • 实现:
    • 检查消息类型是否为 WM_HOTKEY(0x0312)。
    • 如果是热键消息,从 wParam 中提取热键ID。
    • keymap 中查找对应的回调函数并执行。
    • handled 设置为 true,表示消息已处理。
GetWindowHandleAndSource
  • 用途:获取窗口句柄和 HwndSource
  • 参数:
    • window:需要处理的 Window 对象
  • 返回:
    • hwnd:窗口句柄
    • hwndSource:与窗口关联的 HwndSource
  • 实现:
    • 使用 WindowInteropHelper 获取窗口句柄。
    • 使用 HwndSource.FromHwnd 获取 HwndSource,如果获取失败则抛出异常。
Register
  • 用途:注册热键。
  • 参数:
    • window:需要注册热键的 Window 对象
    • modifiers:热键修饰符(例如 CTRLALT 等)
    • key:热键按键
    • callback:热键触发时的回调函数
  • 实现:
    • 调用 GetWindowHandleAndSource 获取窗口句柄和 HwndSource
    • WndProc 注册为窗口消息处理函数。
    • 生成唯一的热键ID。
    • 将按键转换为虚拟键码。
    • 调用 RegisterHotKey 函数注册热键,如果注册失败则抛出异常。
    • 将热键ID和回调函数添加到 keymap 中。
Unregister
  • 用途:注销热键。
  • 参数:
    • window:需要注销热键的 Window 对象
    • callback:需要注销的回调函数
  • 实现:
    • 调用 GetWindowHandleAndSource 获取窗口句柄和 HwndSource
    • 升级 keymapLock,确保线程安全。
    • keymap 中查找对应回调函数的热键ID。
    • 调用 UnRegisterHotKey 函数注销热键。
    • keymap 中移除对应的热键ID和回调函数。

静态方法(外部调用)

RegisterHotKeyUnRegisterHotKey
  • 用途:注册和注销热键的外部调用。
  • 参数:
    • hWnd:窗口句柄
    • id:热键ID
    • fsModifiers:热键修饰符
    • vk:虚拟键码
  • 实现:
    • 使用 DllImport 导入 user32.dll 中的 RegisterHotKeyUnRegisterHotKey 函数。

委托

HotKeyCallbackHandler
  • 用途:定义热键回调函数的委托。
  • 类型:public delegate void HotKeyCallbackHandler()

枚举

HotkeyModifiers
  • 用途:定义热键修饰符。
  • 成员:
    • ALT:0x0001
    • CTRL:0x0002
    • SHIFT:0x0004
    • WIN:0x0008
    • CTRL_ALTCTRL | ALT
    • CTRL_SHIFTCTRL | SHIFT

应用示例

这段代码是 OnSourceInitialized 方法的重写,用于在窗口的 SourceInitialized 事件触发时注册热键。SourceInitialized 事件在窗口句柄创建后立即触发。

        protected override void OnSourceInitialized(EventArgs e){Hotkey.Register(this, HotkeyModifiers.CTRL, Key.D, () =>{WindowController.Get<FlowView>().Hide();WindowController.Get<MainView>().Show();});Hotkey.Register(this, HotkeyModifiers.CTRL_ALT, Key.D, () =>{Application.Current.Shutdown();});}
http://www.lryc.cn/news/513482.html

相关文章:

  • 【笔记】增值税计算笔记
  • 请解释 JavaScript 中的闭包,以及它的优缺点和常见使用场景?
  • SpringBoot 集成 Caffeine 实现本地缓存
  • druid连接池参数配置
  • 【OceanBase】通过 OceanBase 的向量检索技术构建图搜图应用
  • Linux 安装运行gatk的教程
  • 什么是unit l2 norm
  • 手写顺序流程图组件
  • 适配器模式概述
  • Logo设计免费生成器:轻松设计个性化标志
  • 智能停车场车牌识别计费系统
  • 谷歌开通第三方平台OAuth登录及Java对接步骤
  • 人体:精妙绝伦的生命之躯
  • python的urllib模块和http模块
  • Java [后端] 开发日常记录(1)
  • jetbrain 安装 copilot
  • 万里数据库GreatSQL监控解析
  • OpenCV-Python实战(9)——滤波降噪
  • Pytorch | 利用DTA针对CIFAR10上的ResNet分类器进行对抗攻击
  • Linux性能测试简介
  • Kile5支持包的安装
  • 【Ubuntu 系统 之 开启远程桌面SSH登录】
  • MySQL 索引分类及区别与特点
  • 对中文乱码的理解,遇到乱码该怎么办。
  • 《机器学习》从入门到实战——逻辑回归
  • svn不能添加.a文件
  • 23.Java 时间日期扩展(新时间日期、新时间日期格式化与解析、时间戳、计算时间日期差、时间矫正器、时区)
  • C语言渗透和好网站
  • mysql系列7—Innodb的redolog
  • 静态时序分析:线负载模型的选择机制