Repository navigation
Expand file tree
/
Copy pathConsoleKeyChordConverter.cs
More file actions
296 lines (269 loc) · 12.2 KB
/
Copy pathConsoleKeyChordConverter.cs
File metadata and controls
296 lines (269 loc) · 12.2 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
284
285
286
287
288
289
290
291
292
293
294
295
296
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Runtime.InteropServices;
using Microsoft.PowerShell.PSReadLine;
namespace Microsoft.PowerShell
{
/// <summary>
/// A helper class for converting strings to ConsoleKey chords.
/// </summary>
public static class ConsoleKeyChordConverter
{
private static Exception CantConvert(string s, params object[] args)
{
return new ArgumentException(string.Format(CultureInfo.CurrentCulture, s, args));
}
/// <summary>
/// Converts a string to a sequence of ConsoleKeyInfo.
/// Sequences are separated by ','.
/// Modifiers appear before the modified key and are separated by '+'.
/// Examples:
/// Ctrl+X
/// Ctrl+D,M
/// Escape,X
/// </summary>
public static ConsoleKeyInfo[] Convert(string chord)
{
if (string.IsNullOrEmpty(chord))
{
throw new ArgumentNullException(nameof(chord));
}
int start = 0;
var first = GetOneKey(chord, ref start);
if (start >= chord.Length)
return new [] { first };
if (chord[start++] != ',')
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
var second = GetOneKey(chord, ref start);
if (start < chord.Length)
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
return new [] { first, second };
}
private static ConsoleKeyInfo GetOneKey(string chord, ref int start)
{
ConsoleModifiers mods = 0;
while (start < chord.Length)
{
var thisMod = GetModifier(chord, ref start);
if (thisMod != 0)
{
// modifier already set?
if ((mods & thisMod) != 0)
{
// Found a duplicate modifier
throw CantConvert(PSReadLineResources.InvalidModifier, thisMod, chord);
}
mods |= thisMod;
if (start >= chord.Length)
{
// Ends with a modifier?
throw CantConvert(PSReadLineResources.UnrecognizedKey, chord);
}
if (chord[start] != '+' && chord[start] != '-')
{
// No character between mod and key
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
}
start++;
continue;
}
// Not a modifier
var comma = chord.IndexOf(",", start, StringComparison.Ordinal);
string input;
if (comma == start)
{
input = ",";
}
else if (comma == -1)
{
input = chord.Substring(start);
}
else
{
input = chord.Substring(start, comma - start);
}
start += input.Length;
if (!MapKeyChar(input, ref mods, out var key, out var keyChar))
{
throw CantConvert(PSReadLineResources.UnrecognizedKey, input);
}
return new ConsoleKeyInfo(keyChar, key,
(mods & ConsoleModifiers.Shift) != 0,
(mods & ConsoleModifiers.Alt) != 0,
(mods & ConsoleModifiers.Control) != 0);
}
throw CantConvert(PSReadLineResources.InvalidSequence, chord);
}
private static ConsoleModifiers GetModifier(string sequence, ref int start)
{
switch (sequence[start])
{
case 's':
case 'S':
if ((sequence.Length - start >= 5) &&
(sequence[start + 1] == 'h' || sequence[start + 1] == 'H') &&
(sequence[start + 2] == 'i' || sequence[start + 2] == 'I') &&
(sequence[start + 3] == 'f' || sequence[start + 3] == 'F') &&
(sequence[start + 4] == 't' || sequence[start + 4] == 'T'))
{
start += 5;
return ConsoleModifiers.Shift;
}
goto default;
case 'c':
case 'C':
if (sequence.Length - start >= 7 &&
(sequence[start + 1] == 'o' || sequence[start + 1] == 'O') &&
(sequence[start + 2] == 'n' || sequence[start + 2] == 'N') &&
(sequence[start + 3] == 't' || sequence[start + 3] == 'T') &&
(sequence[start + 4] == 'r' || sequence[start + 4] == 'R') &&
(sequence[start + 5] == 'o' || sequence[start + 5] == 'O') &&
(sequence[start + 6] == 'l' || sequence[start + 6] == 'L'))
{
start += 7;
return ConsoleModifiers.Control;
}
else if (sequence.Length - start >= 4 &&
(sequence[start + 1] == 't' || sequence[start + 1] == 'T') &&
(sequence[start + 2] == 'r' || sequence[start + 2] == 'R') &&
(sequence[start + 3] == 'l' || sequence[start + 3] == 'L'))
{
start += 4;
return ConsoleModifiers.Control;
}
goto default;
case 'a':
case 'A':
if (sequence.Length - start >= 3 &&
(sequence[start + 1] == 'l' || sequence[start + 1] == 'L') &&
(sequence[start + 2] == 't' || sequence[start + 2] == 'T'))
{
start += 3;
return ConsoleModifiers.Alt;
}
goto default;
default:
return 0;
}
}
struct KeyPair
{
public char KeyChar;
public ConsoleKey Key;
}
private static readonly Dictionary<string, KeyPair> KeyMappings =
new Dictionary<string, KeyPair>(StringComparer.OrdinalIgnoreCase)
{
{ "Backspace", new KeyPair { Key = ConsoleKey.Backspace } }, // Not '\b' - ^H and BS differ on Linux
{ "Delete", new KeyPair { Key = ConsoleKey.Delete } },
{ "DownArrow", new KeyPair { Key = ConsoleKey.DownArrow} },
{ "End", new KeyPair { Key = ConsoleKey.End } },
{ "Enter", new KeyPair { Key = ConsoleKey.Enter } }, // Platforms differ - '\r' or '\n'
{ "Escape", new KeyPair { Key = ConsoleKey.Escape, KeyChar = '\x1b' } },
{ "F1", new KeyPair { Key = ConsoleKey.F1 } },
{ "F2", new KeyPair { Key = ConsoleKey.F2 } },
{ "F3", new KeyPair { Key = ConsoleKey.F3 } },
{ "F4", new KeyPair { Key = ConsoleKey.F4 } },
{ "F5", new KeyPair { Key = ConsoleKey.F5 } },
{ "F6", new KeyPair { Key = ConsoleKey.F6 } },
{ "F7", new KeyPair { Key = ConsoleKey.F7 } },
{ "F8", new KeyPair { Key = ConsoleKey.F8 } },
{ "F9", new KeyPair { Key = ConsoleKey.F9 } },
{ "F10", new KeyPair { Key = ConsoleKey.F10 } },
{ "F11", new KeyPair { Key = ConsoleKey.F11 } },
{ "F12", new KeyPair { Key = ConsoleKey.F12 } },
{ "F13", new KeyPair { Key = ConsoleKey.F13 } },
{ "F14", new KeyPair { Key = ConsoleKey.F14 } },
{ "F15", new KeyPair { Key = ConsoleKey.F15 } },
{ "F16", new KeyPair { Key = ConsoleKey.F16 } },
{ "F17", new KeyPair { Key = ConsoleKey.F17 } },
{ "F18", new KeyPair { Key = ConsoleKey.F18 } },
{ "F19", new KeyPair { Key = ConsoleKey.F19 } },
{ "F20", new KeyPair { Key = ConsoleKey.F20 } },
{ "F21", new KeyPair { Key = ConsoleKey.F21 } },
{ "F22", new KeyPair { Key = ConsoleKey.F22 } },
{ "F23", new KeyPair { Key = ConsoleKey.F23 } },
{ "F24", new KeyPair { Key = ConsoleKey.F24 } },
{ "Home", new KeyPair { Key = ConsoleKey.Home } },
{ "Insert", new KeyPair { Key = ConsoleKey.Insert } },
{ "LeftArrow", new KeyPair { Key = ConsoleKey.LeftArrow} },
{ "PageDown", new KeyPair { Key = ConsoleKey.PageDown} },
{ "PageUp", new KeyPair { Key = ConsoleKey.PageUp} },
{ "RightArrow", new KeyPair { Key = ConsoleKey.RightArrow} },
{ "Spacebar", new KeyPair { Key = ConsoleKey.Spacebar, KeyChar = ' ' } },
{ "Tab", new KeyPair { Key = ConsoleKey.Tab, KeyChar = '\t' } },
{ "UpArrow", new KeyPair { Key = ConsoleKey.UpArrow} },
};
static bool MapKeyChar(string input, ref ConsoleModifiers mods, out ConsoleKey key, out char keyChar)
{
var isCtrl = (mods & ConsoleModifiers.Control) != 0;
if (input.Length == 1)
{
keyChar = input[0];
if (keyChar >= 'a' && keyChar <= 'z')
{
key = ConsoleKey.A + (keyChar - 'a');
}
else if (keyChar >= 'A' && keyChar <= 'Z')
{
key = ConsoleKey.A + (keyChar - 'A');
mods |= ConsoleModifiers.Shift;
}
else if (keyChar >= '0' && keyChar <= '9')
{
key = ConsoleKey.D0 + (keyChar - '0');
}
else
{
key = 0;
}
if (isCtrl)
{
switch (keyChar)
{
case var _ when (keyChar >= 'a' && keyChar <= 'z'):
keyChar = (char)(keyChar - 'a' + 1);
break;
case var _ when (keyChar >= 'A' && keyChar <= ']'):
keyChar = (char)(keyChar - 'A' + 1);
break;
case '_':
keyChar = '\x1f';
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
mods |= ConsoleModifiers.Shift;
}
break;
case '^':
keyChar = '\x1e';
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
mods |= ConsoleModifiers.Shift;
}
break;
}
}
return true;
}
if (KeyMappings.TryGetValue(input, out var keyPair))
{
key = keyPair.Key;
if (key == ConsoleKey.Backspace)
{
keyChar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) == isCtrl
? '\x7f' : '\x08';
}
else
{
keyChar = keyPair.KeyChar;
}
return true;
}
key = 0;
keyChar = '\0';
return Enum.TryParse(input, out key) && !int.TryParse(input, out var asInt);
}
}
}