-
Notifications
You must be signed in to change notification settings - Fork 0
/
HotkeyControl.cs
594 lines (534 loc) · 19.5 KB
/
HotkeyControl.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
/*
* Copyright (c) 2012-2022 Jens-Uwe Rossbach
*
* This code is licensed under the MIT License.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace ClockIn
{
/// <summary>
/// A simple control that allows the user to select pretty much any valid hotkey combination.
/// </summary>
public class HotkeyControl : TextBox
{
private const uint VirtualKeyToVirtualScanCode = 0x00;
private const uint VirtualScanCodeToVirtualKey = 0x01;
private const uint VirtualKeyToVirtualChar = 0x02;
private const uint VirtualScanCodeToVirtualKeyEx = 0x03;
private const uint VirtualKeyToVirtualScanCodeEx = 0x04;
// These variables store the current hotkey and modifier(s)
private Keys keyCode = Keys.None;
private Keys keyModifiers = Keys.None;
// ArrayLists used to enforce the use of proper modifiers.
// Shift+A isn't a valid hotkey, for instance, as it would screw up when the user is typing.
private ArrayList needNonShiftModifier = new ArrayList();
private ArrayList needNonAltGrModifier = new ArrayList();
private ContextMenu emptyContextMenu = new ContextMenu();
[DllImport("user32.dll")]
private static extern uint MapVirtualKey(uint uCode, uint uMapType);
/// <summary>
/// Creates a new HotkeyControl.
/// </summary>
public HotkeyControl()
{
ContextMenu = emptyContextMenu; // Disable right-clicking
Text = Properties.Resources.KeyNone;
// Handle events that occurs when keys are pressed
KeyPress += new KeyPressEventHandler(HotkeyControl_KeyPress);
KeyUp += new KeyEventHandler(HotkeyControl_KeyUp);
KeyDown += new KeyEventHandler(HotkeyControl_KeyDown);
// Fill the ArrayLists that contain all invalid hotkey combinations
PopulateModifierLists();
}
/// <summary>
/// Used to make sure that there is no right-click menu available.
/// </summary>
public override ContextMenu ContextMenu
{
get => emptyContextMenu;
set => base.ContextMenu = emptyContextMenu;
}
/// <summary>
/// Forces the control to be non-multiline.
/// </summary>
public override bool Multiline
{
get => base.Multiline;
set => base.Multiline = false; // ignore what the user wants, force Multiline to false
}
/// <summary>
/// Used to get/set the hotkey.
/// </summary>
public Keys Hotkey
{
get => ClockIn.Hotkey.Merge(keyModifiers, keyCode);
set
{
ClockIn.Hotkey.Split(value, out keyModifiers, out keyCode);
Render(true);
}
}
/// <summary>
/// Used to get/set the hotkey key code (e.g. Keys.A).
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Keys KeyCode
{
get => keyCode;
set
{
keyCode = value;
Render(true);
}
}
/// <summary>
/// Used to get/set the hotkey modifiers (e.g. Keys.Alt | Keys.Control).
/// </summary>
[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public Keys KeyModifiers
{
get => keyModifiers;
set
{
keyModifiers = value;
Render(true);
}
}
/// <summary>
/// Clears the current hotkey and resets the TextBox.
/// </summary>
public new void Clear()
{
ResetHotkey();
}
/// <summary>
/// Clears the current hotkey and resets the TextBox.
/// </summary>
public void ResetHotkey()
{
Console.WriteLine("HotkeyControl: ResetHotkey");
keyCode = Keys.None;
keyModifiers = Keys.None;
Render();
}
/// <summary>
/// Handles some misc keys, such as Ctrl+Delete and Shift+Insert.
/// </summary>
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if ((keyData == Keys.Delete) || (keyData == (Keys.Control | Keys.Delete)))
{
ResetHotkey();
return true;
}
if (keyData == (Keys.Shift | Keys.Insert)) // Paste
{
return true; // Don't allow
}
// Allow the rest
return base.ProcessCmdKey(ref msg, keyData);
}
/// <summary>
/// Populates the ArrayLists specifying disallowed hotkeys
/// such as Shift+A, Ctrl+Alt+4 (would produce a dollar sign), etc.
/// </summary>
private void PopulateModifierLists()
{
// Shift + 0 - 9, A - Z
for (Keys k = Keys.D0; k <= Keys.Z; k++)
{
needNonShiftModifier.Add((int)k);
}
// Shift + Numpad keys
for (Keys k = Keys.NumPad0; k <= Keys.NumPad9; k++)
{
needNonShiftModifier.Add((int)k);
}
// Shift + Misc (,;<./ etc)
for (Keys k = Keys.Oem1; k <= Keys.OemBackslash; k++)
{
needNonShiftModifier.Add((int)k);
}
// Shift + Space, PgUp, PgDn, End, Home
for (Keys k = Keys.Space; k <= Keys.Home; k++)
{
needNonShiftModifier.Add((int)k);
}
// Misc keys that we can't loop through
needNonShiftModifier.Add((int)Keys.Insert);
needNonShiftModifier.Add((int)Keys.Help);
needNonShiftModifier.Add((int)Keys.Multiply);
needNonShiftModifier.Add((int)Keys.Add);
needNonShiftModifier.Add((int)Keys.Subtract);
needNonShiftModifier.Add((int)Keys.Divide);
needNonShiftModifier.Add((int)Keys.Decimal);
needNonShiftModifier.Add((int)Keys.Return);
needNonShiftModifier.Add((int)Keys.Escape);
needNonShiftModifier.Add((int)Keys.NumLock);
needNonShiftModifier.Add((int)Keys.Scroll);
needNonShiftModifier.Add((int)Keys.Pause);
// Ctrl+Alt + 0 - 9
for (Keys k = Keys.D0; k <= Keys.D9; k++)
{
needNonAltGrModifier.Add((int)k);
}
}
/// <summary>
/// Helper function
/// </summary>
private void Render()
{
Render(false);
}
/// <summary>
/// Redraws the TextBox when necessary.
/// </summary>
/// <param name="calledProgramatically">
/// Specifies whether this function was called by the Hotkey/HotkeyModifiers properties or by the user.
/// </param>
private void Render(bool calledProgramatically)
{
// No hotkey set
if (keyCode == Keys.None)
{
Text = Properties.Resources.KeyNone;
return;
}
// LWin/RWin doesn't work as hotkeys (neither do they work as modifier keys in .NET 2.0)
if ((keyCode == Keys.LWin) || (keyCode == Keys.RWin))
{
keyCode = Keys.None;
Text = Properties.Resources.KeyNone;
return;
}
// Only validate input if it comes from the user
if (!calledProgramatically)
{
// No modifier or shift only, AND a hotkey that needs another modifier
if (((keyModifiers == Keys.Shift) || (keyModifiers == Keys.None)) &&
needNonShiftModifier.Contains((int)keyCode))
{
if (keyModifiers == Keys.None)
{
// Set Ctrl+Alt as the modifier unless Ctrl+Alt+<key> won't work...
if (!needNonAltGrModifier.Contains((int)keyCode))
{
keyModifiers = Keys.Control | Keys.Alt;
}
else // ... in that case, use Shift+Alt instead.
{
keyModifiers = Keys.Shift | Keys.Alt;
}
}
else
{
// User pressed Shift and an invalid key (e.g. a letter or a number),
// that needs another set of modifier keys
keyCode = Keys.None;
Text = Properties.Resources.KeyInvalid;
return;
}
}
// Check all Ctrl+Alt keys
if ((keyModifiers == (Keys.Control | Keys.Alt)) &&
needNonAltGrModifier.Contains((int)keyCode))
{
// Ctrl+Alt+4 etc. won't work; reset hotkey and tell the user
keyCode = Keys.None;
Text = Properties.Resources.KeyInvalid;
return;
}
}
if (keyModifiers == Keys.None)
{
if (keyCode == Keys.None)
{
Text = Properties.Resources.KeyNone;
return;
}
else
{
// We get here if we've got a hotkey that is valid without a modifier,
// like F1-F12, Media-keys etc.
Text = HotkeyToString(keyCode);
return;
}
}
// I have no idea why this is needed, but it is. Without this code, pressing only Ctrl
// will show up as "Control + ControlKey", etc.
if ((keyCode == Keys.Menu) /* Alt */ || (keyCode == Keys.ShiftKey) || (keyCode == Keys.ControlKey))
{
keyCode = Keys.None;
}
if (keyCode == Keys.None)
{
Text = ModifiersToString(keyModifiers);
}
else
{
Text = ModifiersToString(keyModifiers) + " + " + HotkeyToString(keyCode);
}
}
/// <summary>
/// Converts modifiers into a string representation.
/// </summary>
/// <param name="modifiers">Modifiers to be converted</param>
/// <returns>String representation of the modifiers</returns>
private string ModifiersToString(Keys modifiers)
{
string str = string.Empty;
if ((modifiers & Keys.Control) != Keys.None)
{
str = Properties.Resources.KeyControl;
}
if ((modifiers & Keys.Shift) != Keys.None)
{
if (str.Length > 0)
{
str += " + ";
}
str += Properties.Resources.KeyShift;
}
if ((modifiers & Keys.Alt) != Keys.None)
{
if (str.Length > 0)
{
str += " + ";
}
str += Properties.Resources.KeyAlt;
}
return str;
}
/// <summary>
/// Converts a raw key into a string representation.
/// </summary>
/// <param name="hotKey">Raw key to be converted</param>
/// <returns>String representation of the raw key</returns>
private string HotkeyToString(Keys hotKey)
{
string str = null;
switch (hotKey)
{
case Keys.D0:
case Keys.D1:
case Keys.D2:
case Keys.D3:
case Keys.D4:
case Keys.D5:
case Keys.D6:
case Keys.D7:
case Keys.D8:
case Keys.D9:
{
str = string.Empty + KeyCodeToChar(hotKey);
break;
}
case Keys.NumPad0:
case Keys.NumPad1:
case Keys.NumPad2:
case Keys.NumPad3:
case Keys.NumPad4:
case Keys.NumPad5:
case Keys.NumPad6:
case Keys.NumPad7:
case Keys.NumPad8:
case Keys.NumPad9:
case Keys.Divide:
case Keys.Multiply:
case Keys.Add:
case Keys.Subtract:
case Keys.Decimal:
{
str = KeyCodeToChar(hotKey) + " (" + Properties.Resources.KeyNumpad + ")";
break;
}
case Keys.Oem1:
case Keys.Oem2:
case Keys.Oem3:
case Keys.Oem4:
case Keys.Oem5:
case Keys.Oem6:
case Keys.Oem7:
case Keys.Oem8:
case Keys.OemBackslash:
case Keys.OemClear:
case Keys.Oemcomma:
case Keys.OemMinus:
case Keys.OemPeriod:
case Keys.Oemplus:
{
str = (string.Empty + KeyCodeToChar(hotKey)).ToUpper();
break;
}
case Keys.Insert:
{
str = Properties.Resources.KeyInsert;
break;
}
case Keys.Delete:
{
str = Properties.Resources.KeyDelete;
break;
}
case Keys.Home:
{
str = Properties.Resources.KeyHome;
break;
}
case Keys.End:
{
str = Properties.Resources.KeyEnd;
break;
}
case Keys.PageUp:
{
str = Properties.Resources.KeyPageUp;
break;
}
case Keys.PageDown:
{
str = Properties.Resources.KeyPageDown;
break;
}
case Keys.Enter:
{
str = Properties.Resources.KeyEnter;
break;
}
case Keys.Left:
{
str = Properties.Resources.KeyLeft;
break;
}
case Keys.Right:
{
str = Properties.Resources.KeyRight;
break;
}
case Keys.Up:
{
str = Properties.Resources.KeyUp;
break;
}
case Keys.Down:
{
str = Properties.Resources.KeyDown;
break;
}
case Keys.Escape:
{
str = Properties.Resources.KeyEscape;
break;
}
case Keys.PrintScreen:
{
str = Properties.Resources.KeyPrint;
break;
}
case Keys.Pause:
{
str = Properties.Resources.KeyPause;
break;
}
case Keys.Tab:
{
str = Properties.Resources.KeyTabulator;
break;
}
case Keys.NumLock:
{
str = Properties.Resources.KeyNumLock;
break;
}
case Keys.CapsLock:
{
str = Properties.Resources.KeyCapsLock;
break;
}
case Keys.Scroll:
{
str = Properties.Resources.KeyScrollLock;
break;
}
default:
{
str = hotKey.ToString();
break;
}
}
return str;
}
/// <summary>
/// Converts a key code into a character.
/// </summary>
/// <param name="keyCode">Key code to be converted</param>
/// <returns>Key code as character</returns>
private char KeyCodeToChar(Keys keyCode)
{
return (char)MapVirtualKey((uint)keyCode, VirtualKeyToVirtualChar);
}
/// <summary>
/// Fires when a key is pushed down. Here, we'll want to update the text in the box
/// to notify the user what combination is currently pressed.
/// </summary>
private void HotkeyControl_KeyDown(object sender, KeyEventArgs e)
{
// Clear the current hotkey
if ((e.KeyCode == Keys.Back) || (e.KeyCode == Keys.Delete))
{
ResetHotkey();
return;
}
else
{
keyModifiers = e.Modifiers;
keyCode = e.KeyCode;
Render();
}
}
/// <summary>
/// Fires when all keys are released. If the current hotkey isn't valid, reset it.
/// Otherwise, do nothing and keep the text and hotkey as it was.
/// </summary>
private void HotkeyControl_KeyUp(object sender, KeyEventArgs e)
{
if ((keyCode == Keys.None) && (Control.ModifierKeys == Keys.None))
{
ResetHotkey();
return;
}
}
/// <summary>
/// Prevents the letter/whatever entered to show up in the TextBox
/// Without this, a "A" key press would appear as "aControl, Alt + A".
/// </summary>
private void HotkeyControl_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
}
}