在C#中创建全局热键

82 篇文章 3 订阅

在C#中创建全局热键通常涉及使用Windows提供的平台特定功能。在C#中,您可以使用Windows API的`RegisterHotKey`函数来创建全局热键。以下是如何创建全局热键的详细步骤:


using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class GlobalHotkey
{
    private const int MOD_ALT = 0x0001;    // Alt键
    private const int MOD_CTRL = 0x0002;   // Ctrl键
    private const int MOD_SHIFT = 0x0004;  // Shift键
    private const int MOD_WIN = 0x0008;    // Windows键
    private const int WM_HOTKEY = 0x0312;

    private Action<object, EventArgs> hotkeyAction;
    private int id;

    [DllImport("user32.dll")]
    private static extern bool RegisterHotKey(IntPtr hWnd, int id, int fsModifiers, Keys vk);

    [DllImport("user32.dll")]
    private static extern bool UnregisterHotKey(IntPtr hWnd, int id);

    public GlobalHotkey(Keys key, int modifier, Action<object, EventArgs> action)
    {
        hotkeyAction = action;
        id = this.GetHashCode();

        RegisterHotKey(Application.OpenForms[0].Handle, id, modifier, key);
        Application.AddMessageFilter(new MessageFilter(this));
    }

    public void Unregister()
    {
        UnregisterHotKey(Application.OpenForms[0].Handle, id);
    }

    private class MessageFilter : IMessageFilter
    {
        private GlobalHotkey hotkey;

        public MessageFilter(GlobalHotkey hotkey)
        {
            this.hotkey = hotkey;
        }

        public bool PreFilterMessage(ref Message m)
        {
            if (m.Msg == WM_HOTKEY && (int)m.WParam == hotkey.id)
            {
                hotkey.hotkeyAction(null, EventArgs.Empty);
                return true;
            }
            return false;
        }
    }
}
 

下面是如何在Windows窗体应用程序中使用`GlobalHotkey`类来注册全局热键的示例:


using System;
using System.Windows.Forms;

public partial class MainForm : Form
{
    public MainForm()
    {
        InitializeComponent();
        // 注册全局热键 (Ctrl + F1) 并定义触发时要执行的操作
        new GlobalHotkey(Keys.F1, GlobalHotkey.MOD_CTRL, (s, e) =>
        {
            MessageBox.Show("Ctrl + F1 被按下!");
        });
    }
}
 

在这个示例中,我们定义了一个`GlobalHotkey`类,该类封装了注册和处理全局热键的逻辑。我们在`MainForm`构造函数中注册热键,并指定触发热键时执行的操作。`GlobalHotkey`类负责在应用程序中全局注册和处理热键。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C# 窗体实现全局热键可以使用 Windows API 函数来实现。以下是一个示例代码: ```csharp using System; using System.Diagnostics; using System.Runtime.InteropServices; using System.Windows.Forms; public partial class MainForm : Form { // 定义全局热键的 ID,可以为任意值,只要不重复即可 private const int HOTKEY_ID = 9000; // 定义 Windows API 函数 [DllImport("user32.dll")] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32.dll")] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); // 定义热键的组合键和热键对应的键码 private const uint MOD_ALT = 0x0001; private const uint VK_F1 = 0x70; public MainForm() { InitializeComponent(); // 注册全局热键 RegisterHotKey(this.Handle, HOTKEY_ID, MOD_ALT, VK_F1); } protected override void WndProc(ref Message m) { base.WndProc(ref m); // 如果收到全局热键的消息 if (m.Msg == 0x0312 && m.WParam.ToInt32() == HOTKEY_ID) { Debug.WriteLine("Hotkey pressed"); } } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); // 注销全局热键 UnregisterHotKey(this.Handle, HOTKEY_ID); } } ``` 在上面的示例,我们定义了一个 `MainForm` 窗体,并在构造函数注册了一个全局热键热键的组合键为 Alt + F1,热键的 ID 为 9000。当用户按下热键时,我们会在控制台输出一条消息。 在窗体的 `WndProc` 方法,我们判断收到的消息是否是全局热键的消息,如果是,就执行相应的操作。在窗体关闭时,我们需要注销全局热键,以释放资源。 需要注意的是,全局热键需要在 Windows 消息循环进行注册和注销,因此必须在窗体实现 `WndProc` 方法,并在其处理相应的消息。同时,全局热键的组合键和热键对应的键码可以根据需求进行修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijingguang

有钱捧个钱场,没钱捧个人场

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值