IKeyboard.cs 5.2 KB

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