namespace Terminal.Gui.Drivers;
///
/// Extension methods for .
///
public static class ConsoleKeyInfoExtensions
{
///
/// Returns a string representation of the suitable for debugging and logging.
///
/// The ConsoleKeyInfo to convert to string.
/// A formatted string showing the key, character, and modifiers.
///
///
/// Examples:
///
/// - Key: A ('a') - lowercase 'a' pressed
/// - Key: A ('A'), Modifiers: Shift - uppercase 'A' pressed
/// - Key: A (\0), Modifiers: Control - Ctrl+A (no printable char)
/// - Key: Enter (0x000D) - Enter key (carriage return)
/// - Key: F5 (\0) - F5 function key
/// - Key: D2 ('@'), Modifiers: Shift - Shift+2 on US keyboard
/// - Key: None ('é') - Accented character
/// - Key: CursorUp (\0), Modifiers: Shift | Control - Ctrl+Shift+Up Arrow
///
///
///
public static string ToString (this ConsoleKeyInfo consoleKeyInfo)
{
var sb = new StringBuilder ();
// Always show the ConsoleKey enum value
sb.Append ("Key: ");
sb.Append (consoleKeyInfo.Key);
// Show the character if it's printable, otherwise show hex representation
sb.Append (" (");
if (consoleKeyInfo.KeyChar >= 32 && consoleKeyInfo.KeyChar <= 126) // Printable ASCII range
{
sb.Append ('\'');
sb.Append (consoleKeyInfo.KeyChar);
sb.Append ('\'');
}
else if (consoleKeyInfo.KeyChar == 0)
{
sb.Append ("\\0");
}
else
{
// Show special characters or non-printable as hex
sb.Append ("0x");
sb.Append (((int)consoleKeyInfo.KeyChar).ToString ("X4"));
}
sb.Append (')');
// Show modifiers if any are set
if (consoleKeyInfo.Modifiers != 0)
{
sb.Append (", Modifiers: ");
var needsSeparator = false;
if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
{
sb.Append ("Shift");
needsSeparator = true;
}
if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
{
if (needsSeparator)
{
sb.Append (" | ");
}
sb.Append ("Alt");
needsSeparator = true;
}
if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0)
{
if (needsSeparator)
{
sb.Append (" | ");
}
sb.Append ("Control");
}
}
return sb.ToString ();
}
}