IKeyboard.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #nullable enable
  2. namespace Terminal.Gui.App;
  3. /// <summary>
  4. /// Defines a contract for managing keyboard input and key bindings at the Application level.
  5. /// <para>
  6. /// This interface decouples keyboard handling state from the static <see cref="Application"/> class,
  7. /// enabling parallelizable unit tests and better testability.
  8. /// </para>
  9. /// </summary>
  10. public interface IKeyboard
  11. {
  12. /// <summary>
  13. /// Sets the application instance that this keyboard handler is associated with.
  14. /// This provides access to application state without coupling to static Application class.
  15. /// </summary>
  16. IApplication? Application { get; set; }
  17. /// <summary>
  18. /// Called when the user presses a key (by the <see cref="IDriver"/>). Raises the cancelable
  19. /// <see cref="KeyDown"/> event, then calls <see cref="View.NewKeyDownEvent"/> on all top level views, and finally
  20. /// if the key was not handled, invokes any Application-scoped <see cref="KeyBindings"/>.
  21. /// </summary>
  22. /// <remarks>Can be used to simulate key press events.</remarks>
  23. /// <param name="key"></param>
  24. /// <returns><see langword="true"/> if the key was handled.</returns>
  25. bool RaiseKeyDownEvent (Key key);
  26. /// <summary>
  27. /// Called when the user releases a key (by the <see cref="IDriver"/>). Raises the cancelable
  28. /// <see cref="KeyUp"/>
  29. /// event
  30. /// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="RaiseKeyDownEvent"/>.
  31. /// </summary>
  32. /// <remarks>Can be used to simulate key release events.</remarks>
  33. /// <param name="key"></param>
  34. /// <returns><see langword="true"/> if the key was handled.</returns>
  35. bool RaiseKeyUpEvent (Key key);
  36. /// <summary>
  37. /// Invokes any commands bound at the Application-level to <paramref name="key"/>.
  38. /// </summary>
  39. /// <param name="key"></param>
  40. /// <returns>
  41. /// <see langword="null"/> if no command was found; input processing should continue.
  42. /// <see langword="false"/> if the command was invoked and was not handled (or cancelled); input processing should continue.
  43. /// <see langword="true"/> if the command was invoked the command was handled (or cancelled); input processing should stop.
  44. /// </returns>
  45. bool? InvokeCommandsBoundToKey (Key key);
  46. /// <summary>
  47. /// Invokes an Application-bound command.
  48. /// </summary>
  49. /// <param name="command">The Command to invoke</param>
  50. /// <param name="key">The Application-bound Key that was pressed.</param>
  51. /// <param name="binding">Describes the binding.</param>
  52. /// <returns>
  53. /// <see langword="null"/> if no command was found; input processing should continue.
  54. /// <see langword="false"/> if the command was invoked and was not handled (or cancelled); input processing should continue.
  55. /// <see langword="true"/> if the command was invoked the command was handled (or cancelled); input processing should stop.
  56. /// </returns>
  57. /// <exception cref="NotSupportedException"></exception>
  58. bool? InvokeCommand (Command command, Key key, KeyBinding binding);
  59. /// <summary>
  60. /// Raised when the user presses a key.
  61. /// <para>
  62. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  63. /// additional processing.
  64. /// </para>
  65. /// </summary>
  66. /// <remarks>
  67. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Unix) do not support firing the
  68. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  69. /// <para>Fired after <see cref="KeyDown"/> and before <see cref="KeyUp"/>.</para>
  70. /// </remarks>
  71. event EventHandler<Key>? KeyDown;
  72. /// <summary>
  73. /// Raised when the user releases a key.
  74. /// <para>
  75. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  76. /// additional processing.
  77. /// </para>
  78. /// </summary>
  79. /// <remarks>
  80. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Unix) do not support firing the
  81. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  82. /// <para>Fired after <see cref="KeyDown"/>.</para>
  83. /// </remarks>
  84. event EventHandler<Key>? KeyUp;
  85. /// <summary>Gets the Application-scoped key bindings.</summary>
  86. KeyBindings KeyBindings { get; }
  87. /// <summary>Gets or sets the key to quit the application.</summary>
  88. Key QuitKey { get; set; }
  89. /// <summary>Gets or sets the key to activate arranging views using the keyboard.</summary>
  90. Key ArrangeKey { get; set; }
  91. /// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
  92. Key NextTabGroupKey { get; set; }
  93. /// <summary>Alternative key to navigate forwards through views. Tab is the primary key.</summary>
  94. Key NextTabKey { get; set; }
  95. /// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
  96. Key PrevTabGroupKey { get; set; }
  97. /// <summary>Alternative key to navigate backwards through views. Shift+Tab is the primary key.</summary>
  98. Key PrevTabKey { get; set; }
  99. }