随笔 - 317, 文章 - 0, 评论 - 453, 阅读 - 114万
  博客园  :: 首页  :: 新随笔  :: 订阅 订阅  :: 管理

C# 系统热键

Posted on   PHP-张工  阅读(2081)  评论(1编辑  收藏  举报

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

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

HotKey类代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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;
    }
}
调用方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

点击右上角即可分享
微信分享提示