注册系统热键
已Ctrl+F1为例子
Hotkey.cs
FrmHotKeys.cs
Hotkey.cs
1using System;
2using System.Runtime.InteropServices;
3
4namespace SystemHotKey
5{
6 public delegate void HotkeyEventHandler(int HotKeyID);
7
8 public class Hotkey : System.Windows.Forms.IMessageFilter
9 {
10 System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
11 IntPtr hWnd;
12
13 public event HotkeyEventHandler OnHotkey;
14
15 public enum KeyFlags
16 {
17 MOD_ALT = 0x1,
18 MOD_CONTROL = 0x2,
19 MOD_SHIFT = 0x4,
20 MOD_WIN = 0x8
21 }
22 [DllImport("user32.dll")]
23 public static extern UInt32 RegisterHotKey( IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
24
25 [DllImport("user32.dll")]
26 public static extern UInt32 UnregisterHotKey( IntPtr hWnd, UInt32 id);
27
28 [DllImport("kernel32.dll")]
29 public static extern UInt32 GlobalAddAtom( String lpString );
30
31 [DllImport("kernel32.dll")]
32 public static extern UInt32 GlobalDeleteAtom( UInt32 nAtom );
33
34 public Hotkey(IntPtr hWnd)
35 {
36 this.hWnd = hWnd;
37 System.Windows.Forms.Application.AddMessageFilter(this);
38 }
39
40 public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
41 {
42 UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
43 RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
44 keyIDs.Add(hotkeyid, hotkeyid);
45 return (int)hotkeyid;
46 }
47
48 public void UnregisterHotkeys()
49 {
50 System.Windows.Forms.Application.RemoveMessageFilter(this);
51 foreach (UInt32 key in keyIDs.Values)
52 {
53 UnregisterHotKey(hWnd, key);
54 GlobalDeleteAtom(key);
55 }
56 }
57
58 public bool PreFilterMessage(ref System.Windows.Forms.Message m)
59 {
60 if (m.Msg == 0x312)
61 {
62 if(OnHotkey != null)
63 {
64 foreach (UInt32 key in keyIDs.Values)
65 {
66 if((UInt32)m.WParam == key)
67 {
68 OnHotkey((int)m.WParam);
69 return true;
70 }
71 }
72 }
73 }
74 return false;
75 }
76 }
77}
2using System.Runtime.InteropServices;
3
4namespace SystemHotKey
5{
6 public delegate void HotkeyEventHandler(int HotKeyID);
7
8 public class Hotkey : System.Windows.Forms.IMessageFilter
9 {
10 System.Collections.Hashtable keyIDs = new System.Collections.Hashtable();
11 IntPtr hWnd;
12
13 public event HotkeyEventHandler OnHotkey;
14
15 public enum KeyFlags
16 {
17 MOD_ALT = 0x1,
18 MOD_CONTROL = 0x2,
19 MOD_SHIFT = 0x4,
20 MOD_WIN = 0x8
21 }
22 [DllImport("user32.dll")]
23 public static extern UInt32 RegisterHotKey( IntPtr hWnd, UInt32 id, UInt32 fsModifiers, UInt32 vk);
24
25 [DllImport("user32.dll")]
26 public static extern UInt32 UnregisterHotKey( IntPtr hWnd, UInt32 id);
27
28 [DllImport("kernel32.dll")]
29 public static extern UInt32 GlobalAddAtom( String lpString );
30
31 [DllImport("kernel32.dll")]
32 public static extern UInt32 GlobalDeleteAtom( UInt32 nAtom );
33
34 public Hotkey(IntPtr hWnd)
35 {
36 this.hWnd = hWnd;
37 System.Windows.Forms.Application.AddMessageFilter(this);
38 }
39
40 public int RegisterHotkey(System.Windows.Forms.Keys Key, KeyFlags keyflags)
41 {
42 UInt32 hotkeyid = GlobalAddAtom(System.Guid.NewGuid().ToString());
43 RegisterHotKey( (IntPtr)hWnd, hotkeyid, (UInt32)keyflags, (UInt32)Key);
44 keyIDs.Add(hotkeyid, hotkeyid);
45 return (int)hotkeyid;
46 }
47
48 public void UnregisterHotkeys()
49 {
50 System.Windows.Forms.Application.RemoveMessageFilter(this);
51 foreach (UInt32 key in keyIDs.Values)
52 {
53 UnregisterHotKey(hWnd, key);
54 GlobalDeleteAtom(key);
55 }
56 }
57
58 public bool PreFilterMessage(ref System.Windows.Forms.Message m)
59 {
60 if (m.Msg == 0x312)
61 {
62 if(OnHotkey != null)
63 {
64 foreach (UInt32 key in keyIDs.Values)
65 {
66 if((UInt32)m.WParam == key)
67 {
68 OnHotkey((int)m.WParam);
69 return true;
70 }
71 }
72 }
73 }
74 return false;
75 }
76 }
77}
FrmHotKeys.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SystemHotKey;
namespace HotkeySample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmHotkey : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private int HotkeyDemo;
public frmHotkey()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Hotkey h = new Hotkey(this.Handle);
HotkeyDemo = h.RegisterHotkey(Keys.F1, Hotkey.KeyFlags.MOD_CONTROL); //定义快捷键Ctrl+F1
h .OnHotkey += new HotkeyEventHandler(h_OnHotkey);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmHotkey());
}
private void h_OnHotkey(int HotKeyID)
{
if (HotKeyID == HotkeyDemo)
{
MessageBox.Show("按了ctrl+f1");
return;
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using SystemHotKey;
namespace HotkeySample
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class frmHotkey : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private int HotkeyDemo;
public frmHotkey()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
Hotkey h = new Hotkey(this.Handle);
HotkeyDemo = h.RegisterHotkey(Keys.F1, Hotkey.KeyFlags.MOD_CONTROL); //定义快捷键Ctrl+F1
h .OnHotkey += new HotkeyEventHandler(h_OnHotkey);
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows 窗体设计器生成的代码
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new frmHotkey());
}
private void h_OnHotkey(int HotKeyID)
{
if (HotKeyID == HotkeyDemo)
{
MessageBox.Show("按了ctrl+f1");
return;
}
}
}
}