-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
HotKey.cs
161 lines (139 loc) · 4.94 KB
/
HotKey.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Mime;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace UnManaged {
#if ! HOTKEY_PROXY
[Flags]
public enum KeyModifier {
None = 0x0000,
Alt = 0x0001,
Ctrl = 0x0002,
NoRepeat = 0x4000,
Shift = 0x0004,
Win = 0x0008
}
#else
using GlobalHotkeyLib;
#endif
public class GlobalHotKey : IDisposable {
private static Dictionary<int, GlobalHotKey> _dictHotKeyToCalBackProc;
[DllImport("user32.dll")]
private static extern bool RegisterHotKey(IntPtr hWnd, int id, UInt32 fsModifiers, UInt32 vlc);
[DllImport("user32.dll")]
private static extern bool UnregisterHotKey(IntPtr hWnd, int id);
public const int WmHotKey = 0x0312;
private bool _disposed = false;
public static Key ParseStringToKey(string key) {
if (key[0] >= 0 && key[0] <= 9)
key = "D" + key;
Enum.TryParse<System.Windows.Input.Key>(key, out var hot_key);
return hot_key;
}
public Key Key { get; private set; }
public KeyModifier KeyModifiers { get; private set; }
public Action<GlobalHotKey> Action { get; set; }
public int Id { get; set; }
// ******************************************************************
public GlobalHotKey(Key k, KeyModifier keyModifiers, Action<GlobalHotKey> action, bool register = true) {
Key = k;
KeyModifiers = keyModifiers;
Action = action;
if (register)
Register();
}
public GlobalHotKey() { }
private static object lock_obj = new object();
public void UpdateHotKey(Key k, KeyModifier keyModifiers, bool register = true) {
Key = k;
KeyModifiers = keyModifiers;
if (register)
Register();
}
// ******************************************************************
public bool Register() {
if (Id != 0)
Unregister();
int virtualKeyCode = KeyInterop.VirtualKeyFromKey(Key);
Id = virtualKeyCode + ((int)KeyModifiers * 0x10000);
bool result = RegisterHotKey(IntPtr.Zero, Id, (UInt32)KeyModifiers, (UInt32)virtualKeyCode);
lock (lock_obj) {
if (_dictHotKeyToCalBackProc == null) {
_dictHotKeyToCalBackProc = new Dictionary<int, GlobalHotKey>();
ComponentDispatcher.ThreadFilterMessage += new ThreadMessageEventHandler(ComponentDispatcherThreadFilterMessage);
}
}
_dictHotKeyToCalBackProc.Add(Id, this);
if (!result)
throw new Exception("Unable to register hot key");
//Debug.Print(result.ToString() + ", " + Id + ", " + virtualKeyCode);
return result;
}
// ******************************************************************
public void Unregister() {
if (Id == 0)
return;
UnregisterHotKey(IntPtr.Zero, Id);
_dictHotKeyToCalBackProc.Remove(Id);
Id = 0;
}
// ******************************************************************
private static void ComponentDispatcherThreadFilterMessage(ref MSG msg, ref bool handled) {
if (!handled) {
if (msg.message == WmHotKey) {
GlobalHotKey hotKey;
if (_dictHotKeyToCalBackProc.TryGetValue((int)msg.wParam, out hotKey)) {
if (hotKey.Action != null) {
Application.Current.Dispatcher.BeginInvoke((Action)(()=>
hotKey.Action.Invoke(hotKey)
));
}
handled = true;
}
}
}
}
// ******************************************************************
// Implement IDisposable.
// Do not make this method virtual.
// A derived class should not be able to override this method.
public void Dispose() {
Dispose(true);
// This object will be cleaned up by the Dispose method.
// Therefore, you should call GC.SupressFinalize to
// take this object off the finalization queue
// and prevent finalization code for this object
// from executing a second time.
GC.SuppressFinalize(this);
}
// ******************************************************************
// Dispose(bool disposing) executes in two distinct scenarios.
// If disposing equals true, the method has been called directly
// or indirectly by a user's code. Managed and unmanaged resources
// can be _disposed.
// If disposing equals false, the method has been called by the
// runtime from inside the finalizer and you should not reference
// other objects. Only unmanaged resources can be _disposed.
protected virtual void Dispose(bool disposing) {
// Check to see if Dispose has already been called.
if (!this._disposed) {
// If disposing equals true, dispose all managed
// and unmanaged resources.
if (disposing) {
// Dispose managed resources.
Unregister();
}
// Note disposing has been done.
_disposed = true;
}
}
}
// ******************************************************************
// ******************************************************************
}