Skip to content

Commit 21cd4e3

Browse files
daxian-dbwlzybkr
authored andcommitted
Fix a few custom key binding on Windows (#657)
Fix #580.
1 parent 8e7900b commit 21cd4e3

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

PSReadLine/ConsoleKeyChordConverter.cs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -246,17 +246,22 @@ static bool MapKeyChar(string input, ref ConsoleModifiers mods, out ConsoleKey k
246246
}
247247
if (isCtrl)
248248
{
249-
if (keyChar >= 'a' && keyChar <= 'z')
249+
switch (keyChar)
250250
{
251-
keyChar = (char)(keyChar - 'a' + 1);
252-
}
253-
else if (keyChar >= 'A' && keyChar <= 'Z')
254-
{
255-
keyChar = (char)(keyChar - 'A' + 1);
256-
}
257-
else
258-
{
259-
keyChar = '\0';
251+
case var _ when (keyChar >= 'a' && keyChar <= 'z'):
252+
keyChar = (char)(keyChar - 'a' + 1);
253+
break;
254+
case var _ when (keyChar >= 'A' && keyChar <= ']'):
255+
keyChar = (char)(keyChar - 'A' + 1);
256+
break;
257+
case '_':
258+
keyChar = Keys.CtrlUnderbar.KeyChar;
259+
mods |= Keys.CtrlUnderbar.Modifiers;
260+
break;
261+
case '^':
262+
keyChar = Keys.CtrlCaret.KeyChar;
263+
mods |= Keys.CtrlCaret.Modifiers;
264+
break;
260265
}
261266
}
262267

PSReadLine/Keys.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ static ConsoleKeyInfo CtrlAlt(char c)
326326
public static ConsoleKeyInfo CtrlLBracket = Ctrl('\x1b');
327327
public static ConsoleKeyInfo CtrlBackslash = Ctrl('\x1c');
328328
public static ConsoleKeyInfo CtrlRBracket = Ctrl('\x1d');
329+
public static ConsoleKeyInfo CtrlCaret = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? CtrlShift('\x1e') : Ctrl('\x1e');
329330
public static ConsoleKeyInfo CtrlUnderbar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? CtrlShift('\x1f') : Ctrl('\x1f');
330331
public static ConsoleKeyInfo CtrlBackspace = Ctrl(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? '\x7f' : '\x08');
331332
public static ConsoleKeyInfo CtrlDelete = Ctrl(ConsoleKey.Delete); // !Linux
@@ -478,13 +479,13 @@ internal static bool IgnoreKeyChar(this ConsoleKeyInfo key)
478479

479480
private static ConsoleModifiers NormalizeModifiers(this ConsoleKeyInfo key)
480481
{
482+
var keyChar = key.IgnoreKeyChar() ? key.KeyChar : key.NormalizeKeyChar();
481483
var result = key.Modifiers;
482-
if (!char.IsControl(key.KeyChar))
484+
if (!char.IsControl(keyChar))
483485
{
484486
// Ignore Shift state unless it's a control character.
485487
result = result & ~ConsoleModifiers.Shift;
486488
}
487-
488489
return result;
489490
}
490491

@@ -537,7 +538,7 @@ internal static int GetNormalizedHashCode(this ConsoleKeyInfo obj)
537538
// Because a comparison of two ConsoleKeyInfo objects is a comparison of the
538539
// combination of the ConsoleKey and Modifiers, we must combine their hashes.
539540
// Note that if the ConsoleKey is default, we must fall back to the KeyChar,
540-
// otherwise every non-special key will compare as the same.
541+
// otherwise every non-special key will compare as the same.
541542
int h1 = obj.IgnoreKeyChar()
542543
? obj.Key.GetHashCode()
543544
: obj.NormalizeKeyChar().GetHashCode();

0 commit comments

Comments
 (0)