Skip to content

Fix custom key binding on Windows #657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 15 additions & 10 deletions PSReadLine/ConsoleKeyChordConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,17 +246,22 @@ static bool MapKeyChar(string input, ref ConsoleModifiers mods, out ConsoleKey k
}
if (isCtrl)
{
if (keyChar >= 'a' && keyChar <= 'z')
switch (keyChar)
{
keyChar = (char)(keyChar - 'a' + 1);
}
else if (keyChar >= 'A' && keyChar <= 'Z')
{
keyChar = (char)(keyChar - 'A' + 1);
}
else
{
keyChar = '\0';
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 = Keys.CtrlUnderbar.KeyChar;
mods |= Keys.CtrlUnderbar.Modifiers;
break;
case '^':
keyChar = Keys.CtrlCaret.KeyChar;
mods |= Keys.CtrlCaret.Modifiers;
break;
}
}

Expand Down
7 changes: 4 additions & 3 deletions PSReadLine/Keys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ static ConsoleKeyInfo CtrlAlt(char c)
public static ConsoleKeyInfo CtrlLBracket = Ctrl('\x1b');
public static ConsoleKeyInfo CtrlBackslash = Ctrl('\x1c');
public static ConsoleKeyInfo CtrlRBracket = Ctrl('\x1d');
public static ConsoleKeyInfo CtrlCaret = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? CtrlShift('\x1e') : Ctrl('\x1e');
public static ConsoleKeyInfo CtrlUnderbar = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? CtrlShift('\x1f') : Ctrl('\x1f');
public static ConsoleKeyInfo CtrlBackspace = Ctrl(RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? '\x7f' : '\x08');
public static ConsoleKeyInfo CtrlDelete = Ctrl(ConsoleKey.Delete); // !Linux
Expand Down Expand Up @@ -478,13 +479,13 @@ internal static bool IgnoreKeyChar(this ConsoleKeyInfo key)

private static ConsoleModifiers NormalizeModifiers(this ConsoleKeyInfo key)
{
var keyChar = key.IgnoreKeyChar() ? key.KeyChar : key.NormalizeKeyChar();
var result = key.Modifiers;
if (!char.IsControl(key.KeyChar))
if (!char.IsControl(keyChar))
{
// Ignore Shift state unless it's a control character.
result = result & ~ConsoleModifiers.Shift;
}

return result;
}

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