Application.Keyboard.cs 4.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #nullable enable
  2. namespace Terminal.Gui.App;
  3. public static partial class Application // Keyboard handling
  4. {
  5. /// <summary>
  6. /// Static reference to the current <see cref="IApplication"/> <see cref="IKeyboard"/>.
  7. /// </summary>
  8. public static IKeyboard Keyboard
  9. {
  10. get => ApplicationImpl.Instance.Keyboard;
  11. set => ApplicationImpl.Instance.Keyboard = value ??
  12. throw new ArgumentNullException(nameof(value));
  13. }
  14. /// <summary>
  15. /// Called when the user presses a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable
  16. /// <see cref="KeyDown"/> event, then calls <see cref="View.NewKeyDownEvent"/> on all top level views, and finally
  17. /// if the key was not handled, invokes any Application-scoped <see cref="KeyBindings"/>.
  18. /// </summary>
  19. /// <remarks>Can be used to simulate key press events.</remarks>
  20. /// <param name="key"></param>
  21. /// <returns><see langword="true"/> if the key was handled.</returns>
  22. public static bool RaiseKeyDownEvent (Key key) => Keyboard.RaiseKeyDownEvent (key);
  23. /// <summary>
  24. /// Invokes any commands bound at the Application-level to <paramref name="key"/>.
  25. /// </summary>
  26. /// <param name="key"></param>
  27. /// <returns>
  28. /// <see langword="null"/> if no command was found; input processing should continue.
  29. /// <see langword="false"/> if the command was invoked and was not handled (or cancelled); input processing should continue.
  30. /// <see langword="true"/> if the command was invoked the command was handled (or cancelled); input processing should stop.
  31. /// </returns>
  32. public static bool? InvokeCommandsBoundToKey (Key key) => Keyboard.InvokeCommandsBoundToKey (key);
  33. /// <summary>
  34. /// Invokes an Application-bound command.
  35. /// </summary>
  36. /// <param name="command">The Command to invoke</param>
  37. /// <param name="key">The Application-bound Key that was pressed.</param>
  38. /// <param name="binding">Describes the binding.</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. /// <exception cref="NotSupportedException"></exception>
  45. public static bool? InvokeCommand (Command command, Key key, KeyBinding binding) => Keyboard.InvokeCommand (command, key, binding);
  46. /// <summary>
  47. /// Raised when the user presses a key.
  48. /// <para>
  49. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  50. /// additional processing.
  51. /// </para>
  52. /// </summary>
  53. /// <remarks>
  54. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Unix) do not support firing the
  55. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  56. /// <para>Fired after <see cref="KeyDown"/> and before <see cref="KeyUp"/>.</para>
  57. /// </remarks>
  58. public static event EventHandler<Key>? KeyDown
  59. {
  60. add => Keyboard.KeyDown += value;
  61. remove => Keyboard.KeyDown -= value;
  62. }
  63. /// <summary>
  64. /// Called when the user releases a key (by the <see cref="IConsoleDriver"/>). Raises the cancelable
  65. /// <see cref="KeyUp"/>
  66. /// event
  67. /// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="RaiseKeyDownEvent"/>.
  68. /// </summary>
  69. /// <remarks>Can be used to simulate key release events.</remarks>
  70. /// <param name="key"></param>
  71. /// <returns><see langword="true"/> if the key was handled.</returns>
  72. public static bool RaiseKeyUpEvent (Key key) => Keyboard.RaiseKeyUpEvent (key);
  73. /// <summary>Gets the Application-scoped key bindings.</summary>
  74. public static KeyBindings KeyBindings => Keyboard.KeyBindings;
  75. internal static void AddKeyBindings ()
  76. {
  77. if (Keyboard is KeyboardImpl keyboard)
  78. {
  79. keyboard.AddKeyBindings ();
  80. }
  81. }
  82. }