Skip to content

ConsoleKeyInfo improvements. #107410

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

Closed
wants to merge 2 commits into from
Closed
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
22 changes: 20 additions & 2 deletions src/libraries/System.Console/src/System/ConsoleKeyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,28 @@ public ConsoleKeyInfo(char keyChar, ConsoleKey key, bool shift, bool alt, bool c
_mods = 0;
if (shift)
_mods |= ConsoleModifiers.Shift;
if (alt)
else if (alt)
_mods |= ConsoleModifiers.Alt;
if (control)
else if (control)
_mods |= ConsoleModifiers.Control;
}

public ConsoleKeyInfo(char keyChar, ConsoleKey key)
{
// Limit ConsoleKey values to 0 to 255, but don't check whether the
// key is a valid value in our ConsoleKey enum. There are a few
// values in that enum that we didn't define, and reserved keys
// that might start showing up on keyboards in a few years.
if (((int)key) < 0 || ((int)key) > 255)
{
throw new ArgumentOutOfRangeException(nameof(key), SR.ArgumentOutOfRange_ConsoleKey);
}

_keyChar = keyChar;
_key = key;
_mods = 0;
}

public char KeyChar
{
get { return _keyChar; }
Expand Down Expand Up @@ -76,5 +92,7 @@ public override int GetHashCode()
// _mods only has enum defined values for 1,2,4: 3 bits. Use the remaining 8 bits.
return _keyChar | ((int)_key << 16) | ((int)_mods << 24);
}

public override string ToString() => $"keyChar: {KeyChar} key: {Key} mods: {Enum.GetName(typeof(ConsoleModifiers), Modifiers)}";
}
}
Loading