博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

C# 系统热键

Posted on 2011-01-19 11:39  PHP-张工  阅读(2081)  评论(1编辑  收藏  举报

C#中没有设置系统热键的方法,需要调用系统API来实现。

在网上找了段代码,自己实践了一下很好用,记录下来以方便以后使用。

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_CONTROL = 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;
    }
}
调用方法:

private Hotkey hotkey;
private int hotKey_Ctrl_F2;
private void btnHotKey_Click(object sender, EventArgs e)
{
	if (btnHotKey.Text == "注册热键")
	{
		hotkey = new Hotkey(this.Handle);
		//定义热键(Ctrl + F2)
		hotKey_Ctrl_F2 = hotkey.RegisterHotkey(System.Windows.Forms.Keys.F2, Hotkey.KeyFlags.MOD_CONTROL);
		hotkey.OnHotkey += new Hotkey.HotkeyEventHandler(OnHotkey);
		btnHotKey.Text = "注销热键";
	}
	else
	{
		hotkey.UnregisterHotkeys();
		btnHotKey.Text = "注册热键";
	}
}

private void OnHotkey(int HotkeyID)
{
	if (HotkeyID == hotKey_Ctrl_F2)
	{
		this.WindowState = FormWindowState.Normal;
		this.Focus();
		MessageBox.Show("Ctrl+F2");
	}
}

实例下载:https://files.cnblogs.com/zjfree/HotKey.rar

运行环境:WIN2003 + VS2005