新闻详情

新闻详情

首页 / 资讯中心 / 详情

C#:自定义全局热键

发布时间:2026/9/30 17:10:53来源:尧图网络
C#:自定义全局热键
hotkey.csusing System; using System.Runtime.InteropServices; using System.Windows.Forms; public partial class Form1 : Form { // 1. 声明 API [DllImport(user32.dll, SetLastError true)] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport(user32.dll, SetLastError true)] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); // 常量定义 private const int WM_HOTKEY 0x0312; private const uint MOD_CONTROL 0x0002; private const uint MOD_ALT 0x0001; private const uint MOD_SHIFT 0x0004; private const uint MOD_WIN 0x0008; private const uint MOD_NOREPEAT 0x4000; // 防止长按重复触发 private const int HOTKEY_ID 100; // 热键唯一ID public Form1() { // 注意不要在构造函数里注册等窗体句柄创建完成后再注册 this.Load Form1_Load; this.FormClosed Form1_FormClosed; } private void Form1_Load(object sender, EventArgs e) { // 2. 注册热键Ctrl Alt F12 bool success RegisterHotKey(this.Handle, HOTKEY_ID, MOD_CONTROL | MOD_ALT | MOD_NOREPEAT, 0x7B); // 0x7B F12 if (!success) { MessageBox.Show(热键注册失败可能已被其他程序占用。); } } // 3. 重写 WndProc 捕获热键消息 protected override void WndProc(ref Message m) { base.WndProc(ref m); // 必须调用基类 if (m.Msg WM_HOTKEY m.WParam.ToInt32() HOTKEY_ID) { // 在这里写你的业务逻辑建议用 BeginInvoke 异步处理避免阻塞 UI this.BeginInvoke((MethodInvoker)delegate { // 比如呼出窗口、显示通知等 this.Show(); this.WindowState FormWindowState.Normal; this.Activate(); }); } } // 4. 退出时注销热键 private void Form1_FormClosed(object sender, FormClosedEventArgs e) { UnregisterHotKey(this.Handle, HOTKEY_ID); } } static class Program { static void Main() { Application.Run(new Form1()); } }编译C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc /target:winexe /out:hotkey.exe hotkey.cs运行hotkey效果Ctrl Alt F12 显示窗口
网站建设高端定制企业官网
RELATED

相关资讯

更多精彩内容,欢迎继续阅读

较早相关资讯

最新相关资讯

temporal-python-testing - local-setup 2026/10/1 2:42:42

temporal-python-testing - local-setup

Temporal Python 测试本地开发环境设置 使用 pytest 集成和覆盖率跟踪设置本地 Temporal 开发环境的综合指南。 使用 Docker Compose 设置 Temporal 服务器 基本 Docker Compose 配置 # docker-compose.yml version: "3.8"services:temporal:image: temporalio/auto-…

阅读更多 →
terraform-aws-modules - SKILL 2026/10/1 2:42:42

terraform-aws-modules - SKILL

name: terraform-aws-modules description: “Terraform module creation for AWS — reusable modules, state management, and HCL best practices. Use when building or reviewing Terraform AWS infrastructure.” risk: critical source: community date_added: “2026-0…

阅读更多 →
threat-mitigation-mapping - SKILL 2026/10/1 2:42:41

threat-mitigation-mapping - SKILL

name: threat-mitigation-mapping description: “Map identified threats to appropriate security controls and mitigations. Use when prioritizing security investments, creating remediation plans, or validating control effectiveness.” risk: safe source: commun…

阅读更多 →
transformers-js - CONFIGURATION 2026/10/1 2:42:41

transformers-js - CONFIGURATION

环境配置参考 使用 env 对象配置 Transformers.js 行为的完整指南。 目录 概述远程模型配置本地模型配置缓存配置WASM 配置网络与日志控制常见配置模式环境最佳实践 概述 env 对象提供对 Transformers.js 执行、缓存和模型加载的全面控制: import { env } fro…

阅读更多 →
test-guard - SKILL 2026/10/1 2:42:41

test-guard - SKILL

name: “test-guard” description: “Review generated or changed test code against universal testing rules before it ships or is presented for approval.” risk: “critical” source: “community” source_repo: “amElnagdy/guard-skills” source_type: “commun…

阅读更多 →
Vue 项目 Luckysheet 本地导入与刷新丢失排查 2026/10/1 2:42:34

Vue 项目 Luckysheet 本地导入与刷新丢失排查

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

阅读更多 →

今日资讯

本周资讯

本月资讯

看完文章仍有疑问?

联系尧图顾问,获取一对一建站咨询

立即免费咨询 📞 400-888-8888
📞 ✉