-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotkeyHandler.cs
204 lines (162 loc) · 6.35 KB
/
HotkeyHandler.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace EveSwitcher
{
public delegate void HotkeyEventHandler(object sender, HotkeyEventArgs e);
public class HotkeyEventArgs : EventArgs
{
public KeyGesture Gesture { get; }
public bool Handled { get; set; }
public HotkeyEventArgs(KeyGesture gesture)
{
Gesture = gesture;
}
}
public class HotkeyHandler : IDisposable
{
private static int _lastId = 0;
private bool _disposed;
private readonly WindowHelper _windowHelper;
private readonly bool _requireActiveEveClient;
private readonly Window _window;
private readonly IntPtr _handle;
private readonly HwndSource _hwndSource;
private readonly List<RegisteredHotKey> _registeredHotkeys = new List<RegisteredHotKey>();
public event HotkeyEventHandler HotkeyPressed;
public HotkeyHandler(bool requireActiveEveClient)
{
_requireActiveEveClient = requireActiveEveClient;
_windowHelper = new WindowHelper();
// Force window creation to start a message queue, but hide it.
_window = new Window
{
Width = 0,
Height = 0,
WindowStyle = WindowStyle.None,
ShowInTaskbar = false,
ShowActivated = false,
Visibility = Visibility.Hidden,
Title = "Eve Switcher"
};
_window.Show();
_window.Hide();
_handle = new WindowInteropHelper(_window).Handle;
_hwndSource = HwndSource.FromHwnd(_handle);
_hwndSource.AddHook(HwndHook);
}
public bool RegisterHotkey(string gesture)
{
var converter = new KeyGestureConverter();
var keyGesture = (KeyGesture)converter.ConvertFromString(gesture);
return RegisterHotkey(keyGesture);
}
public bool RegisterHotkey(KeyGesture gesture)
{
if (_registeredHotkeys.Any(x => x.Gesture == gesture))
{
throw new ArgumentException("Hotkey already registered.");
}
var vk = KeyInterop.VirtualKeyFromKey(gesture.Key);
int id = GetNextId();
uint fsModifiers = 0;
if ((gesture.Modifiers & ModifierKeys.Alt) == ModifierKeys.Alt)
fsModifiers |= HotkeyNativeMethods.MOD_ALT;
if ((gesture.Modifiers & ModifierKeys.Control) == ModifierKeys.Control)
fsModifiers |= HotkeyNativeMethods.MOD_CONTROL;
if ((gesture.Modifiers & ModifierKeys.Shift) == ModifierKeys.Shift)
fsModifiers |= HotkeyNativeMethods.MOD_SHIFT;
var result = HotkeyNativeMethods.RegisterHotKey(_handle, id, fsModifiers, (uint)vk);
if (result)
{
_registeredHotkeys.Add(new RegisteredHotKey { Id = id, Gesture = gesture, Modifiers = fsModifiers, VirtualKey = (uint)vk });
}
return result;
}
public void UnregisterHotkey(KeyGesture gesture)
{
var hotkey = _registeredHotkeys.FirstOrDefault(x => x.Gesture == gesture);
if (hotkey == null)
throw new ArgumentException("Hotkey not registered.");
HotkeyNativeMethods.UnregisterHotKey(_handle, hotkey.Id);
_registeredHotkeys.Remove(hotkey);
}
private IntPtr HwndHook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
const int WM_HOTKEY = 0x0312;
if (msg == WM_HOTKEY)
{
int id = wParam.ToInt32();
var hotkey = _registeredHotkeys.FirstOrDefault(x => x.Id == id);
if (hotkey != null)
{
if (_requireActiveEveClient && !_windowHelper.IsEveClientActive())
{
handled = false;
}
else
{
var keyEventArgs = new HotkeyEventArgs(hotkey.Gesture);
HotkeyPressed?.Invoke(this, keyEventArgs);
handled = keyEventArgs.Handled;
}
if (!handled)
{
HotkeyNativeMethods.UnregisterHotKey(_handle, hotkey.Id);
KeyboardMessage.Send(hotkey.Gesture);
HotkeyNativeMethods.RegisterHotKey(_handle, hotkey.Id, hotkey.Modifiers, hotkey.VirtualKey);
}
}
}
return IntPtr.Zero;
}
public void Dispose() => Dispose(true);
protected virtual void Dispose(bool disposing)
{
if (_disposed)
{
return;
}
if (disposing)
{
foreach (var hotkey in _registeredHotkeys.ToArray())
{
UnregisterHotkey(hotkey.Gesture);
}
_hwndSource.RemoveHook(HwndHook);
_hwndSource.Dispose();
_window.Close();
}
_disposed = true;
}
private static int GetNextId()
{
return _lastId++;
}
private class RegisteredHotKey
{
public int Id { get; set; }
public KeyGesture Gesture { get; set; }
public uint Modifiers { get; set; }
public uint VirtualKey { get; set; }
}
private static class HotkeyNativeMethods
{
public const uint WM_HOTKEY = 0x0312;
public const uint MOD_ALT = 0x0001;
public const uint MOD_CONTROL = 0x0002;
public const uint MOD_NOREPEAT = 0x4000;
public const uint MOD_SHIFT = 0x0004;
public const uint MOD_WIN = 0x0008;
public const uint ERROR_HOTKEY_ALREADY_REGISTERED = 1409;
[DllImport("user32.dll")]
public static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
[DllImport("user32.dll")]
public static extern bool UnregisterHotKey(IntPtr hWnd, int id);
}
}
}