ConsoleKeyInfoExtensions.cs 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. namespace Terminal.Gui.Drivers;
  2. /// <summary>
  3. /// Extension methods for <see cref="ConsoleKeyInfo"/>.
  4. /// </summary>
  5. public static class ConsoleKeyInfoExtensions
  6. {
  7. /// <summary>
  8. /// Returns a string representation of the <see cref="ConsoleKeyInfo"/> suitable for debugging and logging.
  9. /// </summary>
  10. /// <param name="consoleKeyInfo">The ConsoleKeyInfo to convert to string.</param>
  11. /// <returns>A formatted string showing the key, character, and modifiers.</returns>
  12. /// <remarks>
  13. /// <para>
  14. /// Examples:
  15. /// <list type="bullet">
  16. /// <item><c>Key: A ('a')</c> - lowercase 'a' pressed</item>
  17. /// <item><c>Key: A ('A'), Modifiers: Shift</c> - uppercase 'A' pressed</item>
  18. /// <item><c>Key: A (\0), Modifiers: Control</c> - Ctrl+A (no printable char)</item>
  19. /// <item><c>Key: Enter (0x000D)</c> - Enter key (carriage return)</item>
  20. /// <item><c>Key: F5 (\0)</c> - F5 function key</item>
  21. /// <item><c>Key: D2 ('@'), Modifiers: Shift</c> - Shift+2 on US keyboard</item>
  22. /// <item><c>Key: None ('é')</c> - Accented character</item>
  23. /// <item><c>Key: CursorUp (\0), Modifiers: Shift | Control</c> - Ctrl+Shift+Up Arrow</item>
  24. /// </list>
  25. /// </para>
  26. /// </remarks>
  27. public static string ToString (this ConsoleKeyInfo consoleKeyInfo)
  28. {
  29. var sb = new StringBuilder ();
  30. // Always show the ConsoleKey enum value
  31. sb.Append ("Key: ");
  32. sb.Append (consoleKeyInfo.Key);
  33. // Show the character if it's printable, otherwise show hex representation
  34. sb.Append (" (");
  35. if (consoleKeyInfo.KeyChar >= 32 && consoleKeyInfo.KeyChar <= 126) // Printable ASCII range
  36. {
  37. sb.Append ('\'');
  38. sb.Append (consoleKeyInfo.KeyChar);
  39. sb.Append ('\'');
  40. }
  41. else if (consoleKeyInfo.KeyChar == 0)
  42. {
  43. sb.Append ("\\0");
  44. }
  45. else
  46. {
  47. // Show special characters or non-printable as hex
  48. sb.Append ("0x");
  49. sb.Append (((int)consoleKeyInfo.KeyChar).ToString ("X4"));
  50. }
  51. sb.Append (')');
  52. // Show modifiers if any are set
  53. if (consoleKeyInfo.Modifiers != 0)
  54. {
  55. sb.Append (", Modifiers: ");
  56. var needsSeparator = false;
  57. if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Shift) != 0)
  58. {
  59. sb.Append ("Shift");
  60. needsSeparator = true;
  61. }
  62. if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Alt) != 0)
  63. {
  64. if (needsSeparator)
  65. {
  66. sb.Append (" | ");
  67. }
  68. sb.Append ("Alt");
  69. needsSeparator = true;
  70. }
  71. if ((consoleKeyInfo.Modifiers & ConsoleModifiers.Control) != 0)
  72. {
  73. if (needsSeparator)
  74. {
  75. sb.Append (" | ");
  76. }
  77. sb.Append ("Control");
  78. }
  79. }
  80. return sb.ToString ();
  81. }
  82. }