public static string ToGestureString(this ConsoleKeyInfo key) { var useKeyEnum = key.IgnoreKeyChar(); var mods = key.Modifiers; var sb = new StringBuilder(); var isShift = (mods & ConsoleModifiers.Shift) != 0; var isCtrl = (mods & ConsoleModifiers.Control) != 0; var isAlt = (mods & ConsoleModifiers.Alt) != 0; if (useKeyEnum && isShift) { sb.Append("Shift"); } if (isCtrl) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Ctrl"); } if (isAlt) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Alt"); } if (sb.Length > 0) { sb.Append("+"); } if (useKeyEnum) { sb.Append(key.Key); } else { var c = key.NormalizeKeyChar(); string s; switch (c) { case ' ': s = "Space"; break; case '\x1b': s = "Escape"; break; case '\x1c': s = "\\"; break; case '\x1d': s = "]"; break; case '\x1f': s = "_"; break; case '\x7f': s = "Backspace"; break; // 'Ctrl+h' is represented as (keychar: 0x08, key: 0, mods: Control). In the case of 'Ctrl+h', // we don't want the keychar to be interpreted as 'Backspace'. case '\x08' when !isCtrl: s = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Backspace" : "Ctrl+Backspace"; break; case char _ when c <= 26: s = ((char)((isShift ? 'A' : 'a') + c - 1)).ToString(); break; default: s = c.ToString(); break; } sb.Append(s); } return(sb.ToString()); }
public static string ToGestureString(this ConsoleKeyInfo key) { var useKeyEnum = key.IgnoreKeyChar(); var mods = key.Modifiers; var sb = new StringBuilder(); if (useKeyEnum && (mods & ConsoleModifiers.Shift) != 0) { sb.Append("Shift"); } if ((mods & ConsoleModifiers.Control) != 0) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Ctrl"); } if ((mods & ConsoleModifiers.Alt) != 0) { if (sb.Length > 0) { sb.Append("+"); } sb.Append("Alt"); } if (sb.Length > 0) { sb.Append("+"); } if (useKeyEnum) { sb.Append(key.Key); } else { var c = key.NormalizeKeyChar(); string s; switch (c) { case ' ': s = "Space"; break; case '\x1b': s = "Escape"; break; case '\x1c': s = "\\"; break; case '\x1d': s = "]"; break; case '\x1f': s = "_"; break; case '\x7f': s = "Backspace"; break; case '\x08': s = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "Backspace" : "Ctrl+Backspace"; break; case char _ when c <= 26: s = ((char)('a' + c - 1)).ToString(); break; default: s = c.ToString(); break; } sb.Append(s); } return(sb.ToString()); }