-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGlobalHotkey.cs
148 lines (125 loc) · 4.94 KB
/
GlobalHotkey.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
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Input;
namespace HardTop
{
internal class GlobalHotKey : IDisposable
{
/// <summary>
/// Registers a global hotkey
/// </summary>
/// <param name="aKeyGestureString">e.g. Alt + Shift + Control + Win + S</param>
/// <param name="aAction">Action to be called when hotkey is pressed</param>
/// <returns>true, if registration succeeded, otherwise false</returns>
public static bool RegisterHotKey(string aKeyGestureString, Action aAction)
{
var c = new KeyGestureConverter();
KeyGesture aKeyGesture = (KeyGesture)c.ConvertFrom(aKeyGestureString);
return RegisterHotKey(aKeyGesture.Modifiers, aKeyGesture.Key, aAction);
}
public static bool RegisterHotKey(ModifierKeys aModifier, Key aKey, Action aAction)
{
if (aModifier == ModifierKeys.None)
{
throw new ArgumentException("Modifier must not be ModifierKeys.None");
}
if (aAction is null)
{
throw new ArgumentNullException(nameof(aAction));
}
System.Windows.Forms.Keys aVirtualKeyCode = (System.Windows.Forms.Keys)KeyInterop.VirtualKeyFromKey(aKey);
currentID++;
bool aRegistered = RegisterHotKey(window.Handle, currentID,
(uint)aModifier | MOD_NOREPEAT,
(uint)aVirtualKeyCode);
if (aRegistered)
{
registeredHotKeys.Add(new HotKeyWithAction(aModifier, aKey, aAction));
}
return aRegistered;
}
public void Dispose()
{
// unregister all the registered hot keys.
for (int i = currentID; i > 0; i--) UnregisterHotKey(window.Handle, i);
// dispose the inner native window.
window.Dispose();
}
static GlobalHotKey()
{
window.KeyPressed += (s, e) =>
{
registeredHotKeys.ForEach(x =>
{
if (e.Modifier == x.Modifier && e.Key == x.Key) x.Action();
});
};
}
private static readonly InvisibleWindowForMessages window = new InvisibleWindowForMessages();
private static int currentID;
private static readonly uint MOD_NOREPEAT = 0x4000;
private static readonly List<HotKeyWithAction> registeredHotKeys = new List<HotKeyWithAction>();
private class HotKeyWithAction
{
public HotKeyWithAction(ModifierKeys modifier, Key key, Action action)
{
Modifier = modifier;
Key = key;
Action = action;
}
public ModifierKeys Modifier { get; }
public Key Key { get; }
public Action Action { get; }
}
// Registers a hot key with Windows.
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
// Unregisters the hot key with Windows.
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
private class InvisibleWindowForMessages : System.Windows.Forms.NativeWindow, IDisposable
{
public InvisibleWindowForMessages()
{
CreateHandle(new System.Windows.Forms.CreateParams());
}
private static readonly int WM_HOTKEY = 0x0312;
protected override void WndProc(ref System.Windows.Forms.Message m)
{
base.WndProc(ref m);
if (m.Msg == WM_HOTKEY)
{
var aWPFKey = KeyInterop.KeyFromVirtualKey(((int)m.LParam >> 16) & 0xFFFF);
ModifierKeys modifier = (ModifierKeys)((int)m.LParam & 0xFFFF);
KeyPressed?.Invoke(this, new HotKeyPressedEventArgs(modifier, aWPFKey));
}
}
public class HotKeyPressedEventArgs : EventArgs
{
private readonly ModifierKeys _modifier;
private readonly Key _key;
internal HotKeyPressedEventArgs(ModifierKeys modifier, Key key)
{
_modifier = modifier;
_key = key;
}
public ModifierKeys Modifier
{
get { return _modifier; }
}
public Key Key
{
get { return _key; }
}
}
public event EventHandler<HotKeyPressedEventArgs> KeyPressed;
#region IDisposable Members
public void Dispose()
{
this.DestroyHandle();
}
#endregion
}
}
}