Repository navigation
Expand file tree
/
Copy pathGlobalHotKey.cs
More file actions
179 lines (163 loc) · 6.27 KB
/
Copy pathGlobalHotKey.cs
File metadata and controls
179 lines (163 loc) · 6.27 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
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
using System.Collections.Concurrent;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
namespace FastHotKeyForWPF
{
public static class GlobalHotKey
{
[DllImport("user32.dll")]
internal static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private static IntPtr WindowhWnd = IntPtr.Zero;
private static HwndSource? source;
public static bool IsAwaked { get; private set; } = false;
private static Dictionary<int, IHotKeyComponent> Components { get; set; } = [];
private static ConcurrentQueue<Tuple<uint, uint, ICollection<HotKeyEventHandler>>> WaitToBeRegisteredInvisible { get; set; } = [];
private static ConcurrentQueue<Tuple<uint, uint, IHotKeyComponent>> WaitToBeRegisteredVisual { get; set; } = [];
internal const int WM_HOTKEY = 0x0312;
private static IntPtr WhileKeyInvoked(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case WM_HOTKEY:
int id = wParam.ToInt32();
if (Components.TryGetValue(id, out var component))
{
component.Invoke();
}
handled = true;
break;
}
return IntPtr.Zero;
}
public static void Awake()
{
if (IsAwaked) return;
WindowhWnd = new WindowInteropHelper(Application.Current.MainWindow).Handle;
if (WindowhWnd != IntPtr.Zero)
{
source = HwndSource.FromHwnd(WindowhWnd);
source.AddHook(new HwndSourceHook(WhileKeyInvoked));
IsAwaked = true;
while (WaitToBeRegisteredInvisible.TryDequeue(out var meta))
{
Register(meta.Item1, meta.Item2, [.. meta.Item3]);
}
while (WaitToBeRegisteredVisual.TryDequeue(out var meta))
{
#if NETFRAMEWORK
var hash = FrameworkSupport.HashCombine(meta.Item1, meta.Item2);
#elif NET
var hash = HashCode.Combine(meta.Item1, meta.Item2);
#endif
RegisterHotKey(WindowhWnd, hash, meta.Item1, meta.Item2);
if (Components.TryGetValue(hash, out _))
{
Components[hash] = meta.Item3;
}
else
{
Components.Add(hash, meta.Item3);
}
}
}
}
public static void Dispose()
{
if (!IsAwaked) return;
foreach (var component in Components)
{
UnregisterHotKey(WindowhWnd, component.Key);
}
Components.Clear();
source?.RemoveHook(new HwndSourceHook(WhileKeyInvoked));
source?.Dispose();
IsAwaked = false;
}
public static int Register(IHotKeyComponent component)
{
if (component.VirtualKeys == 0x0000 || component.VirtualModifiers == 0x0000) return -1;
if (IsAwaked)
{
#if NETFRAMEWORK
var id = FrameworkSupport.HashCombine(component.VirtualModifiers, component.VirtualKeys);
#elif NET
var id = HashCode.Combine(component.VirtualModifiers, component.VirtualKeys);
#endif
UnregisterHotKey(WindowhWnd, id);
if (Components.TryGetValue(id, out var same))
{
Components.Remove(id);
same.Covered();
}
if (RegisterHotKey(WindowhWnd, id, component.VirtualModifiers, component.VirtualKeys))
{
Components.Add(id, component);
return id;
}
else
{
return -1;
}
}
else
{
WaitToBeRegisteredVisual.Enqueue(Tuple.Create(component.VirtualModifiers, component.VirtualKeys, component));
return 0;
}
}
public static int Register(uint modifiers, uint triggers, params HotKeyEventHandler[] handlers)
{
if (modifiers == 0x0000 || triggers == 0x0000) return -1;
if (IsAwaked)
{
#if NETFRAMEWORK
var id = FrameworkSupport.HashCombine(modifiers, triggers);
#elif NET
var id = HashCode.Combine(modifiers, triggers);
#endif
Unregister(modifiers, triggers);
var reg = RegisterHotKey(WindowhWnd, id, modifiers, triggers);
if (reg)
{
var component = new InvisibleHotkeyComponent(modifiers, triggers);
foreach (var handler in handlers)
{
component.Handler += handler;
}
Components.Add(id, component);
}
return reg ? id : -1;
}
else
{
WaitToBeRegisteredInvisible.Enqueue(Tuple.Create(modifiers, triggers, handlers as ICollection<HotKeyEventHandler>));
return 0;
}
}
public static int Register(VirtualModifiers modifierKeys, VirtualKeys triggerKeys, params HotKeyEventHandler[] handlers)
{
return Register((uint)modifierKeys, (uint)triggerKeys, handlers);
}
public static bool Unregister(uint modifiers, uint triggers)
{
#if NETFRAMEWORK
var id = FrameworkSupport.HashCombine(modifiers, triggers);
#elif NET
var id = HashCode.Combine(modifiers, triggers);
#endif
var ureg = UnregisterHotKey(WindowhWnd, id);
if (Components.TryGetValue(id, out _))
{
Components.Remove(id);
}
return ureg;
}
public static bool Unregister(VirtualModifiers modifierKeys, VirtualKeys triggerKeys)
{
return Unregister((uint)modifierKeys, (uint)triggerKeys);
}
}
}