-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotKeyManager7.0.cs
190 lines (170 loc) · 5.37 KB
/
HotKeyManager7.0.cs
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System.Runtime.InteropServices;
namespace MyMineBlock
{
public sealed partial class HotKeyManager : IDisposable
{
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[LibraryImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool UnregisterHotKey(IntPtr hWnd, int id);
private readonly static int WM_HOTKEY = 0x0312;
private class Window : NativeWindow, IDisposable
{
public Window()
{
CreateHandle(new CreateParams());
}
protected override void WndProc(ref Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
Keys key = (Keys)(((int)m.LParam >> 16) & 0xFFFF);
ModKeys modifier = (ModKeys)((int)m.LParam & 0xFFFF);
windowKeyPressed?.Invoke(this, new KeyPressedEventArgs(modifier, key));
}
}
public event EventHandler<KeyPressedEventArgs>? windowKeyPressed;
#region IDisposable Members
public void Dispose()
{
DestroyHandle();
}
#endregion
}
readonly private Window listenerWindow = new();
private int currentId = 0;
public HotKeyManager()
{
listenerWindow.windowKeyPressed += (HotKeySender, args) =>
{
if (args is not null)
{
keyPressed?.Invoke(this, args);
}
};
}
private readonly List<HotKey> registeredHotKeys = new();
public void RegisterHotKey(ModKeys modifier, Keys key)
{
if (RegisterHotKey(listenerWindow.Handle, currentId, (uint)modifier, (uint)key))
{
HotKey newHotKey = new()
{
Modifier = modifier,
Key = key,
Id = currentId
};
registeredHotKeys.Add(newHotKey);
currentId++;
}
}
public void RegisterHotKey(ModKeys[] modifiers, Keys key)
{
uint fsModifiers = 0;
foreach (ModKeys modifier in modifiers)
{
fsModifiers |= (uint)modifier;
}
if (RegisterHotKey(listenerWindow.Handle, currentId, fsModifiers, (uint)key))
{
HotKey newHotKey = new()
{
Modifiers = modifiers,
Key = key,
Id = currentId
};
registeredHotKeys.Add(newHotKey);
currentId++;
}
}
public void RegisterHotKeyNoRepeat(ModKeys modifier, Keys key)
{
uint fsModifiers = (uint)modifier | (uint)ModKeys.NoRepeat;
if (RegisterHotKey(listenerWindow.Handle, currentId, fsModifiers, (uint)key))
{
HotKey newHotKey = new()
{
Modifier = modifier,
Key = key,
Id = currentId
};
registeredHotKeys.Add(newHotKey);
currentId++;
}
}
public void RegisterHotKeyNoRepeat(ModKeys[] modifiers, Keys key)
{
uint fsModifiers = 0;
foreach (ModKeys modifier in modifiers)
{
if (modifier != ModKeys.NoRepeat)
{
fsModifiers |= (uint)modifier;
}
}
fsModifiers |= (uint)ModKeys.NoRepeat;
if (RegisterHotKey(listenerWindow.Handle, currentId, fsModifiers, (uint)key))
{
HotKey newHotKey = new()
{
Modifiers = modifiers,
Key = key,
Id = currentId
};
registeredHotKeys.Add(newHotKey);
currentId++;
}
}
#region IDisposable Members
public void Dispose()
{
for (int i = currentId; i > -1; i--)
{
UnregisterHotKey(listenerWindow.Handle, i);
}
currentId = 0;
registeredHotKeys.Clear();
keyPressed = null;
}
#endregion
public static event EventHandler<KeyPressedEventArgs>? keyPressed;
}
public class KeyPressedEventArgs : EventArgs
{
private readonly ModKeys _modkey;
private readonly Keys _key;
public ModKeys Modkey
{
get { return _modkey; }
}
public Keys Key
{
get { return _key; }
}
internal KeyPressedEventArgs(ModKeys modkey, Keys key)
{
_modkey = modkey;
_key = key;
}
}
public struct HotKey
{
public ModKeys Modifier { get; set; }
public ModKeys[] Modifiers { get; set; }
public Keys Key { get; set; }
public int Id { get; set; }
}
[Flags]
public enum ModKeys : uint
{
None = 0,
Alt = 1,
Control = 2,
Shift = 4,
Win = 8,
NoRepeat = 16384
}
}