-
-
Notifications
You must be signed in to change notification settings - Fork 591
Expand file tree
/
Copy pathHotkeyModel.cs
More file actions
244 lines (214 loc) · 7.09 KB
/
HotkeyModel.cs
File metadata and controls
244 lines (214 loc) · 7.09 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Input;
namespace Flow.Launcher.Infrastructure.Hotkey
{
public record struct HotkeyModel
{
public bool Alt { get; set; }
public bool Shift { get; set; }
public bool Win { get; set; }
public bool Ctrl { get; set; }
public Key CharKey { get; set; } = Key.None;
private static readonly Dictionary<Key, string> specialSymbolDictionary = new Dictionary<Key, string>
{
{ Key.Space, "Space" }, { Key.Oem3, "~" }
};
public ModifierKeys ModifierKeys
{
get
{
ModifierKeys modifierKeys = ModifierKeys.None;
if (Alt)
{
modifierKeys |= ModifierKeys.Alt;
}
if (Shift)
{
modifierKeys |= ModifierKeys.Shift;
}
if (Win)
{
modifierKeys |= ModifierKeys.Windows;
}
if (Ctrl)
{
modifierKeys |= ModifierKeys.Control;
}
return modifierKeys;
}
}
public readonly bool IsEmpty => CharKey == Key.None && !Alt && !Shift && !Win && !Ctrl;
public static HotkeyModel Empty => new();
public HotkeyModel(string hotkeyString)
{
Parse(hotkeyString);
}
public HotkeyModel(bool alt, bool shift, bool win, bool ctrl, Key key)
{
Alt = alt;
Shift = shift;
Win = win;
Ctrl = ctrl;
CharKey = key;
}
private void Parse(string hotkeyString)
{
if (string.IsNullOrEmpty(hotkeyString))
{
return;
}
List<string> keys = hotkeyString.Replace(" ", "").Split('+').ToList();
if (keys.Contains("Alt"))
{
Alt = true;
keys.Remove("Alt");
}
if (keys.Contains("Shift"))
{
Shift = true;
keys.Remove("Shift");
}
if (keys.Contains("Win"))
{
Win = true;
keys.Remove("Win");
}
if (keys.Contains("Ctrl"))
{
Ctrl = true;
keys.Remove("Ctrl");
}
if (keys.Count == 1)
{
string charKey = keys[0];
KeyValuePair<Key, string>? specialSymbolPair =
specialSymbolDictionary.FirstOrDefault(pair => pair.Value == charKey);
if (specialSymbolPair.Value.Value != null)
{
CharKey = specialSymbolPair.Value.Key;
}
else
{
try
{
CharKey = (Key)Enum.Parse(typeof(Key), charKey);
}
catch (ArgumentException)
{
}
}
}
}
public bool Equals(HotkeyModel other)
{
return CharKey == other.CharKey && ModifierKeys == other.ModifierKeys;
}
public KeyGesture ToKeyGesture()
{
return new KeyGesture(CharKey, ModifierKeys);
}
public override readonly string ToString()
{
return string.Join(" + ", EnumerateDisplayKeys());
}
public readonly IEnumerable<string> EnumerateDisplayKeys()
{
if (Ctrl && CharKey is not (Key.LeftCtrl or Key.RightCtrl))
{
yield return "Ctrl";
}
if (Alt && CharKey is not (Key.LeftAlt or Key.RightAlt))
{
yield return "Alt";
}
if (Shift && CharKey is not (Key.LeftShift or Key.RightShift))
{
yield return "Shift";
}
if (Win && CharKey is not (Key.LWin or Key.RWin))
{
yield return "Win";
}
if (CharKey != Key.None)
{
yield return specialSymbolDictionary.TryGetValue(CharKey, out var value)
? value
: CharKey.ToString();
}
}
/// <summary>
/// Validate hotkey
/// </summary>
/// <param name="validateKeyGesture">Try to validate hotkey as a KeyGesture.</param>
/// <returns></returns>
public bool Validate(bool validateKeyGesture = false)
{
switch (CharKey)
{
case Key.LeftAlt:
case Key.RightAlt:
case Key.LeftCtrl:
case Key.RightCtrl:
case Key.LeftShift:
case Key.RightShift:
case Key.LWin:
case Key.RWin:
case Key.None:
return false;
default:
if (validateKeyGesture)
{
try
{
KeyGesture keyGesture = new KeyGesture(CharKey, ModifierKeys);
}
catch (System.Exception e) when
(e is NotSupportedException || e is InvalidEnumArgumentException)
{
return false;
}
}
if (ModifierKeys == ModifierKeys.None)
{
return !IsPrintableCharacter(CharKey);
}
else
{
return true;
}
}
}
private static bool IsPrintableCharacter(Key key)
{
// https://stackoverflow.com/questions/11881199/identify-if-a-event-key-is-text-not-only-alphanumeric
return (key >= Key.A && key <= Key.Z) ||
(key >= Key.D0 && key <= Key.D9) ||
(key >= Key.NumPad0 && key <= Key.NumPad9) ||
key == Key.OemQuestion ||
key == Key.OemQuotes ||
key == Key.OemPlus ||
key == Key.OemOpenBrackets ||
key == Key.OemCloseBrackets ||
key == Key.OemMinus ||
key == Key.DeadCharProcessed ||
key == Key.Oem1 ||
key == Key.Oem7 ||
key == Key.OemPeriod ||
key == Key.OemComma ||
key == Key.OemMinus ||
key == Key.Add ||
key == Key.Divide ||
key == Key.Multiply ||
key == Key.Subtract ||
key == Key.Oem102 ||
key == Key.Decimal;
}
public override int GetHashCode()
{
return HashCode.Combine(ModifierKeys, CharKey);
}
}
}