C#全局热键

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinFormsApp10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:
                             MessageBox.Show("F3");

                            break;
                        case 101:
                            {
                             MessageBox.Show("F4");

                            }
                            break;

                    }
                    break;
            }
            base.WndProc(ref m);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            CsAPI.RegisterHotKey(Handle, 100, CsAPI.KeyModifiers.None, Keys.F3);
            CsAPI.RegisterHotKey(Handle, 101, CsAPI.KeyModifiers.None, Keys.F4);
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            CsAPI.UnregisterHotKey(Handle, 101);
            CsAPI.UnregisterHotKey(Handle, 100);
        }
    }
}

CsAPI.cs

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;

class CsAPI
    {

    //如果函数执行成功,返回值不为0。
    //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    public static extern bool RegisterHotKey(IntPtr hWnd,                //要定义热键的窗口的句柄
       int id,                     //定义热键ID(不能与其它ID重复)
        KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
        Keys vk                     //定义热键的内容
        );
    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    public static extern bool UnregisterHotKey(
        IntPtr hWnd,                //要取消热键的窗口的句柄
        int id                      //要取消热键的ID
        );
    //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
    [Flags()]
    public enum KeyModifiers
    {
        None = 0,
        Alt = 1,
        Ctrl = 2,
        Shift = 4,
        WindowsKey = 8
    }
}

源码下载地址 全局热键.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值