-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotKeyControl.cs
More file actions
283 lines (258 loc) · 9.98 KB
/
HotKeyControl.cs
File metadata and controls
283 lines (258 loc) · 9.98 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using static NvkCommon.Log;
namespace NvkCommon
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Interoperability", "CA1416:Validate platform compatibility", Justification = "<Pending>")]
public partial class HotKeyControl : UserControl
{
private static readonly string TAG = NvkCommon.Log.TAG(typeof(HotKeyControl));
public event EventHandler HotkeySequenceMatched;
public event EventHandler HotkeySequenceRecorded;
public List<RawInputCapture.KeyEventInfo> HotkeySequence
{
get
{
return hotkeySequenceActual;
}
set
{
hotkeySequenceActual.Clear();
if (value != null)
{
hotkeySequenceActual.AddRange(value);
}
HookStart();
}
}
private bool HasKeyBinds
{
get
{
return hotkeySequenceActual.Count > 0;
}
}
private bool _isKeyBindRecording;
private bool IsKeyBindRecording
{
get
{
return _isKeyBindRecording;
}
set
{
if (value)
{
if (!_isKeyBindRecording)
{
_isKeyBindRecording = true;
buttonPredefinedKeyBindAdd.Text = "*";
HotkeySequenceTextUpdate();
HookStart();
}
}
else
{
if (_isKeyBindRecording)
{
_isKeyBindRecording = false;
buttonPredefinedKeyBindAdd.Text = "+";
HotkeySequenceTextUpdate();
if (!HasKeyBinds)
{
HookStop();
}
}
}
}
}
private readonly List<RawInputCapture.KeyEventInfo> hotkeySequenceActual = [];
private readonly List<RawInputCapture.KeyEventInfo> hotkeySequencePrevious = [];
private readonly List<RawInputCapture.KeyEventInfo> hotkeySequenceTest = [];
private readonly RawInputCapture rawInputCapture;
public HotKeyControl()
{
InitializeComponent();
if (DesignMode) return;
rawInputCapture = new RawInputCapture(Name);
// BUG: revisit this; it is stopping the listener when the app deactivates
// (which it should only do if there are no bindings),
// and never resuming when the app re-activates. :/
//ParentForm.Deactivate += ParentForm_Deactivate;
//ParentForm.Activated += ParentForm_Activated;
}
/*
~HotKeyControl()
{
HookStop();
}
*/
public void WndProc(ref Message m)
{
rawInputCapture?.WndProc(ref m);
}
private void HotKeyControl_Load(object sender, EventArgs e)
{
textBoxPredefinedKeyBind.BackColor = Color.Green;
textBoxPredefinedKeyBind.ForeColor = Color.White;
HotkeySequenceTextUpdate();
}
private void HotkeySequenceTextUpdate()
{
if (IsKeyBindRecording)
{
if (HasKeyBinds)
{
textBoxPredefinedKeyBind.Text = Utils.ToString(hotkeySequenceActual);
}
else
{
textBoxPredefinedKeyBind.Text = "Recording...";
}
textBoxPredefinedKeyBind.BackColor = Color.Red;
textBoxPredefinedKeyBind.ForeColor = Color.White;
}
else
{
if (HasKeyBinds)
{
textBoxPredefinedKeyBind.Text = Utils.ToString(hotkeySequenceActual);
textBoxPredefinedKeyBind.BackColor = Color.Green;
textBoxPredefinedKeyBind.ForeColor = Color.White;
}
else
{
textBoxPredefinedKeyBind.Text = "Press + to record";
textBoxPredefinedKeyBind.BackColor = Color.Yellow;
textBoxPredefinedKeyBind.ForeColor = Color.Black;
}
}
}
private void Log(LogLevel level, string message)
{
NvkCommon.Log.PrintLine(TAG, level, $"{Utils.Quote(Name)} {message}");
}
public void HookStart()
{
Log(LogLevel.Debug, $"HookStart()");
if (!HasKeyBinds && !IsKeyBindRecording)
{
Log(LogLevel.Debug, $"HookStart: No key binds and not recording; ignoring");
return;
}
if (rawInputCapture.IsCapturingKeyboard) return;
Log(LogLevel.Debug, $"HookStart: HasKeyBinds || IsKeyBindRecording; starting");
HotkeyListenerTestReset();
rawInputCapture.OnKeyboard += RawInputCapture_OnKeyboard;
rawInputCapture.StartKeyboard(FindForm().Handle);
}
public void HookStop()
{
Log(LogLevel.Debug, $"HookStop()");
rawInputCapture.OnKeyboard -= RawInputCapture_OnKeyboard;
HotkeyListenerTestReset();
rawInputCapture.StopKeyboard(FindForm().Handle);
if (IsKeyBindRecording)
{
IsKeyBindRecording = false;
}
}
private void RawInputCapture_OnKeyboard(object sender, RawInputCapture.KeyboardEventArgs e)
{
//Log(LogLevel.Information, $"RawInputCapture_OnKeyboard: e={e}");
//Log(LogLevel.Information, $"KeyboardHook_KeyIntercepted: e.KeyEventInfo={e.KeyEventInfo}");
//Log(LogLevel.Information, $"KeyboardHook_KeyIntercepted: e.CapsLockOn={e.IsCapsLockOn}");
var keyEventInfoActual = e.KeyEventInfo;
//Log(LogLevel.Verbose, $"RawInputCapture_OnKeyboard: keyEventInfoActual={keyEventInfoActual}");
if (IsKeyBindRecording)
{
hotkeySequenceActual.Add(keyEventInfoActual);
HotkeySequenceTextUpdate();
}
else
{
if (hotkeySequenceTest.Count > 0)
{
var reset = true;
var hotkeySequenceTestNext = hotkeySequenceTest[0];
//Log(LogLevel.Verbose, $"RawInputCapture_OnKeyboard: hotkeySequenceTestNext={hotkeySequenceTestNext}");
if (keyEventInfoActual == hotkeySequenceTestNext)
{
hotkeySequenceTest.RemoveAt(0);
Log(LogLevel.Debug, $"RawInputCapture_OnKeyboard: MATCHED");
//Log(LogLevel.Verbose, $"RawInputCapture_OnKeyboard: Remaining: hotkeySequenceTest({hotkeySequenceTest.Count})={Utils.ToString(hotkeySequenceTest)}");
if (hotkeySequenceTest.Count == 0)
{
Log(LogLevel.Information, $"RawInputCapture_OnKeyboard: MATCHED WHOLE SEQUENCE!");
HotkeySequenceMatched?.Invoke(this, EventArgs.Empty);
}
else
{
reset = false;
}
}
if (reset)
{
HotkeyListenerTestReset();
}
}
else
{
//Log(LogLevel.Verbose, $"RawInputCapture_OnKeyboard: hotkeySequenceTest is empty; ignoring");
}
}
}
private void HotkeyListenerTestReset()
{
if (hotkeySequenceTest != hotkeySequenceActual)
{
hotkeySequenceTest.Clear();
hotkeySequenceTest.AddRange(hotkeySequenceActual);
}
}
private void buttonPredefinedKeyBindAdd_Click(object sender, EventArgs e)
{
if (IsKeyBindRecording)
{
// End recording hotkey sequence
IsKeyBindRecording = false;
hotkeySequencePrevious.Clear();
hotkeySequenceTest.Clear();
HotkeySequenceRecorded?.Invoke(this, EventArgs.Empty);
HotkeyListenerTestReset();
}
else
{
// Start recording hotkey sequence
hotkeySequencePrevious.Clear();
hotkeySequencePrevious.AddRange(hotkeySequenceActual);
hotkeySequenceActual.Clear();
hotkeySequenceTest.Clear();
IsKeyBindRecording = true;
}
}
private void buttonPredefinedKeyBindRemove_Click(object sender, EventArgs e)
{
if (IsKeyBindRecording)
{
// Cancel recording hotkey sequence
IsKeyBindRecording = false;
hotkeySequenceActual.Clear();
hotkeySequenceActual.AddRange(hotkeySequencePrevious);
hotkeySequencePrevious.Clear();
hotkeySequenceTest.Clear();
}
else
{
// Remove the saved hotkey sequence
hotkeySequenceActual.Clear();
hotkeySequencePrevious.Clear();
hotkeySequenceTest.Clear();
HotkeySequenceTextUpdate();
}
}
}
}