C#中控制台程序(Console)注册全局快捷键
2024-06-24
34
需求描述
在C#开发应用时,有时候需要注册到快捷键(HotKey),二在控制台程序中由于没有消息循环,无法直接处理快捷键的事件,这里记录一下在控制台程序中,如何注册全局的快捷键。
实现方法
先上代码,HotKeyManager类的代码如下:
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Threading;
namespace ConsoleHotKey
{
public static class HotKeyManager
{
public static event EventHandlerHotKeyEventArgs HotKeyPressed;
public static int RegisterHotKey(Keys key, KeyModifiers modifiers)
{
_windowReadyEvent.WaitOne();
int id = System.Threading.Interlocked.Increment(ref _id);
_wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key);
return id;
}
public static void UnregisterHotKey(int id)
{
_wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id);
}
delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key);
delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id);
private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key)
{
RegisterHotKey(hwnd, id, modifiers, key);
}
private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id)
{
UnregisterHotKey(_hwnd, id);
}
private static void OnHotKeyPressed(HotKeyEventArgs e)
{
if (HotKeyManager.HotKeyPressed != null)
{
HotKeyManager.HotKeyPressed(null, e);
}
}
private static volatile MessageWindow _wnd;
private static volatile IntPtr _hwnd;
private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false);
static HotKeyManager()
{
Thread messageLoop = new Thread(delegate()
{
Application.Run(new MessageWindow());
});
messageLoop.Name = MessageLoopThread;
messageLoop.IsBackground = true;
messageLoop.Start();
}
private class MessageWindow : Form
{
public MessageWindow()
{
_wnd = this;
_hwnd = this.Handle;
_windowReadyEvent.Set();
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_HOTKEY)
{
HotKeyEventArgs e = new HotKeyEventArgs(m.LParam);
HotKeyManager.OnHotKeyPressed(e);
}
base.WndProc(ref m);
}
protected override void SetVisibleCore(bool value)
{
// Ensure the window never becomes visible
base.SetVisibleCore(false);
}
private const int WM_HOTKEY = 0x312;
}
[DllImport(user32, SetLastError=true)]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport(user32, SetLastError = true)]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static int _id = 0;
}
public class HotKeyEventArgs : EventArgs
{
public readonly Keys Key;
public readonly KeyModifiers Modifiers;
public HotKeyEventArgs(Keys key, KeyModifiers modifiers)
{
this.Key = key;
this.Modifiers = modifiers;
}
public HotKeyEventArgs(IntPtr hotKeyParam)
{
uint param = (uint)hotKeyParam.ToInt64();
Key = (Keys)((param 0xffff0000) 16);
Modifiers = (KeyModifiers)(param 0x0000ffff);
}
}
[Flags]
public enum KeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Windows = 8,
NoRepeat = 0x4000
}
}
以下为使用HotKeyManager在控制台应用程序中的示例:
using System;
using System.Windows.Forms;
namespace ConsoleHotKey
{
class Program
{
static void Main(string[] args)
{
HotKeyManager.RegisterHotKey(Keys.A, KeyModifiers.Alt);
HotKeyManager.HotKeyPressed += new EventHandlerHotKeyEventArgs(HotKeyManager_HotKeyPressed);
Console.ReadLine();
}
static void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e)
{
Console.WriteLine(Hit me!);
}
}
}
这个类在Winform的程序中也可以直接使用。
更新于:2个月前赞一波!2
相关文章
- 小程序中商家入驻提醒、新订单提醒
- 微信小程序中的支付宝支付
- uniapp 微信小程序 控制台警告和错误处理
- 微信小程序内容安全检测(敏感词、敏感图)
- mac 常用快捷键总结
- 微信小程序订阅消息
- 小程序客服会话
- 获取用户授权的手机号【微信小程序】
- EasyWechat 4.x 微信小程序订阅消息
- 小程序测试号、公众号测试号
- EasyWechat 4.x 微信小程序企业付款到零钱
- EasyWechat 3.x 小程序客服消息自动回复
- EasyWeChat 生成小程序码报错 cURL错误 60
- 微信小程序 wx.requestPayment({}) 唤起微信支付
- composer 全局参数
- Windows所有组合快捷键大全
- 人人商城 V3.18.1 小程序端获取商品列表接口
- 在64位的Windows系统下运行32位的C#程序如何控制重定向
- 火爆全网的ChatGPT智能AI机器人微信小程序源码 (附带部署教程)
- C#程序中如何获取当前操作系统的名称
文章评论
全部评论