-
Notifications
You must be signed in to change notification settings - Fork 0
/
ShortcutKey.cs
79 lines (65 loc) · 2.02 KB
/
ShortcutKey.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
using System.Globalization;
using System.Windows.Input;
namespace DeadEye;
public sealed class ShortcutKey
{
public static readonly ModifierKeys[] AllModifiers = { ModifierKeys.Control, ModifierKeys.Shift, ModifierKeys.Alt, ModifierKeys.Windows };
public static readonly Key[] IgnoredKeys =
{
// do not put Key.Escape in here
Key.System,
Key.LeftCtrl, Key.RightCtrl,
Key.LeftShift, Key.RightShift,
Key.LeftAlt, Key.RightAlt,
Key.LWin, Key.RWin,
Key.NumLock, Key.CapsLock, Key.Scroll,
Key.Apps, Key.Sleep,
};
private static readonly Dictionary<ModifierKeys, string> MODIFIER_NAMES = new()
{
{ ModifierKeys.Control, "Ctrl" },
{ ModifierKeys.Shift, "Shift" },
{ ModifierKeys.Alt, "Alt" },
{ ModifierKeys.Windows, "Win" },
};
private static readonly Dictionary<Key, string> KEY_NAMES = new()
{
{ Key.PageUp, "Page Up" },
{ Key.PageDown, "Page Down" },
{ Key.Back, "Backspace" },
{ Key.Enter, "Enter" },
{ Key.PrintScreen, "PrtScr" },
};
[Obsolete("Only used by the serializer", true)]
public ShortcutKey() { }
public ShortcutKey(ModifierKeys modifiers, Key key)
{
this.ModifierKeys = modifiers;
this.Key = key;
}
public ModifierKeys ModifierKeys { get; set; }
public Key Key { get; set; }
public override string ToString()
{
if (this.Key == Key.None)
return "None";
var keyList = new List<string>();
foreach (var modifier in AllModifiers)
{
if (this.ModifierKeys.HasFlag(modifier))
keyList.Add(MODIFIER_NAMES[modifier]);
}
if (!IgnoredKeys.Contains(this.Key))
{
var keyName = CultureInfo.InvariantCulture.TextInfo.ToTitleCase(this.Key.ToString());
if (KEY_NAMES.TryGetValue(this.Key, out var value))
keyName = value;
else if (this.Key is >= Key.D0 and <= Key.D9)
keyName = (this.Key - Key.D0).ToString(CultureInfo.InvariantCulture);
else if (this.Key is >= Key.NumPad0 and <= Key.NumPad9)
keyName = "Numpad " + (this.Key - Key.NumPad0).ToString(CultureInfo.InvariantCulture);
keyList.Add(keyName);
}
return string.Join("+", keyList);
}
}