【C#】注册全局热键 监听按键 Hotkey

      

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


public class Hotkey : IMessageFilter
{
    public delegate void HotkeyEventHandler(int HotKeyID);
    public event HotkeyEventHandler OnHotkey;
    private Hashtable keyIDs = new Hashtable();
    private IntPtr hWnd;

    /// <summary>
    /// 辅助按键
    /// </summary>
    public enum KeyFlags
    {
        MOD_NULL = 0x0,
        MOD_ALT = 0x1,
        MOD_CTRL = 0x2,
        MOD_SHIFT = 0x4,
        MOD_WIN = 0x8
    }

    /// <summary>
    /// 注册热键API
    /// </summary>
    [DllImport("user32.dll")]
    public static extern UInt32 RegisterHotKey(IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);

    /// <summary>
    /// 注销热键API
    /// </summary>
    [DllImport("user32.dll")]
    public static extern UInt32 UnregisterHotKey(IntPtr hWnd, UInt32 id);

    /// <summary>
    /// 全局原子表添加原子
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern UInt32 GlobalAddAtom(String lpString);

    /// <summary>
    /// 全局原子表删除原子
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom);

    /// <summary>
    /// 构造函数
    /// </summary>
    /// <param name="hWnd">当前句柄</param>
    public Hotkey(IntPtr hWnd)
    {
        this.hWnd = hWnd;
        Application.AddMessageFilter(this);
    }

    /// <summary>
    /// 注册热键
    /// </summary>
    public int RegisterHotkey(Keys Key, KeyFlags keyflags)
    {
        UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
        RegisterHotKey((IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
        keyIDs.Add(hotkeyid, hotkeyid);
        return (int)hotkeyid;
    }

    /// <summary>
    /// 注销所有热键
    /// </summary>
    public void UnregisterHotkeys()
    {
        Application.RemoveMessageFilter(this);
        foreach (UInt32 key in keyIDs.Values)
        {
            UnregisterHotKey(hWnd, key);
            GlobalDeleteAtom(key);
        }
    }

    /// <summary>
    /// 消息筛选
    /// </summary>
    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x312)
        {
            if (OnHotkey != null)
            {
                foreach (UInt32 key in keyIDs.Values)
                {
                    if ((UInt32)m.WParam == key)
                    {
                        OnHotkey((int)m.WParam);
                        return true;
                    }
                }
            }
        }
        return false;
    }
}



#region 注册热键
        private Hotkey hotkey;
        private int hotKey_F4;
        private int hotKey_F5;
        private int hotKey_F6;
        private int hotKey_F7;
        private int hotKey_F3;
        private void HotKey_signin(object sender, EventArgs e)
        {
            //if (btnHotKey.Text == "注册热键")
            //{
            hotkey = new Hotkey(this.Handle);
            //定义热键
            hotKey_F4 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F4, Hotkey.KeyFlags.MOD_NULL);
            hotKey_F5 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F5, Hotkey.KeyFlags.MOD_NULL);
            hotKey_F6 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F6, Hotkey.KeyFlags.MOD_NULL);
            hotKey_F7 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F7, Hotkey.KeyFlags.MOD_NULL);
            hotKey_F3 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F3, Hotkey.KeyFlags.MOD_NULL);

            hotkey.OnHotkey += new Hotkey.HotkeyEventHandler(OnHotkey);
            //btnHotKey.Text = "注销热键";
            //}
            //else
            //{
            //    hotkey.UnregisterHotkeys();
            //    btnHotKey.Text = "注册热键";
            //}
        }

        private void OnHotkey(int HotkeyID)
        {
            if (HotkeyID == hotKey_F5)
            {
                MessageBox.Show("F5");
            }
            else if (HotkeyID == hotKey_F4)
            {
                MessageBox.Show("F4");
            }
            else if (HotkeyID == hotKey_F6)
            {
                btn_manlookout_Click(null, null);
                MessageBox.Show("F6");
            }
            else if (HotkeyID == hotKey_F7)
            {
                MessageBox.Show("F7");
            }
            else if (HotkeyID == hotKey_F3)
            {
                MessageBox.Show("F3");
            }
        }
    #endregion 注册热键


本文出自《粉墨记忆》 => 《【C#】注册全局热键 监听按键 Hotkey
转载时请注明出处及相应链接, 本文地址:https://www.fmxk.ac.cn/?post=2

WRITTEN BY

avatar


发表评论: