-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathHotKeys.cs
More file actions
62 lines (58 loc) · 1.83 KB
/
Copy pathHotKeys.cs
File metadata and controls
62 lines (58 loc) · 1.83 KB
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
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace PostMessage.NET.GUI
{
class HotKeys
{
// Import API
[DllImport("user32.dll")]
static extern bool RegisterHotKey(IntPtr hWnd, int id, int modifiers, Keys vk);
[DllImport("user32.dll")]
static extern bool UnregisterHotKey(IntPtr hWnd, int id);
// Composite keys
public enum HotkeyModifiers
{
Alt = 1,
Control = 2,
Shift = 4,
Win = 8,
}
int keyid = 10;
public delegate void HotKeyCallBackHanlder();
Dictionary<int, HotKeyCallBackHanlder> keymap = new Dictionary<int, HotKeyCallBackHanlder>();
// Register HotKeys
public void Regist(IntPtr hWnd, int modifiers, Keys vk, HotKeyCallBackHanlder callBack)
{
int id = keyid++;
if (!RegisterHotKey(hWnd, id, modifiers, vk))
throw new Exception("Failed to register hotkeys!");
keymap[id] = callBack;
}
// Unregister HotKeys
public void UnRegist(IntPtr hWnd, HotKeyCallBackHanlder callBack)
{
foreach (KeyValuePair<int, HotKeyCallBackHanlder> var in keymap)
{
if (var.Value == callBack)
{
UnregisterHotKey(hWnd, var.Key);
return;
}
}
}
// HotKeys Event Handle
public void ProcessHotKey(Message m)
{
if (m.Msg == 0x312)
{
int id = m.WParam.ToInt32();
HotKeyCallBackHanlder callback;
if (keymap.TryGetValue(id, out callback))
callback();
}
}
}
}