Application.Keyboard.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using System.Text.Json.Serialization;
  2. namespace Terminal.Gui;
  3. public static partial class Application // Keyboard handling
  4. {
  5. private static Key _alternateForwardKey = Key.Empty; // Defined in config.json
  6. /// <summary>Alternative key to navigate forwards through views. Ctrl+Tab is the primary key.</summary>
  7. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  8. [JsonConverter (typeof (KeyJsonConverter))]
  9. public static Key AlternateForwardKey
  10. {
  11. get => _alternateForwardKey;
  12. set
  13. {
  14. if (_alternateForwardKey != value)
  15. {
  16. Key oldKey = _alternateForwardKey;
  17. _alternateForwardKey = value;
  18. OnAlternateForwardKeyChanged (new (oldKey, value));
  19. }
  20. }
  21. }
  22. private static void OnAlternateForwardKeyChanged (KeyChangedEventArgs e)
  23. {
  24. // TODO: The fact Top has it's own AlternateForwardKey and events is needlessly complex. Remove it.
  25. foreach (Toplevel top in _topLevels.ToArray ())
  26. {
  27. top.OnAlternateForwardKeyChanged (e);
  28. }
  29. }
  30. private static Key _alternateBackwardKey = Key.Empty; // Defined in config.json
  31. /// <summary>Alternative key to navigate backwards through views. Shift+Ctrl+Tab is the primary key.</summary>
  32. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  33. [JsonConverter (typeof (KeyJsonConverter))]
  34. public static Key AlternateBackwardKey
  35. {
  36. get => _alternateBackwardKey;
  37. set
  38. {
  39. if (_alternateBackwardKey != value)
  40. {
  41. Key oldKey = _alternateBackwardKey;
  42. _alternateBackwardKey = value;
  43. OnAlternateBackwardKeyChanged (new (oldKey, value));
  44. }
  45. }
  46. }
  47. private static void OnAlternateBackwardKeyChanged (KeyChangedEventArgs oldKey)
  48. {
  49. // TODO: The fact Top has it's own AlternateBackwardKey and events is needlessly complex. Remove it.
  50. foreach (Toplevel top in _topLevels.ToArray ())
  51. {
  52. top.OnAlternateBackwardKeyChanged (oldKey);
  53. }
  54. }
  55. private static Key _quitKey = Key.Empty; // Defined in config.json
  56. /// <summary>Gets or sets the key to quit the application.</summary>
  57. [SerializableConfigurationProperty (Scope = typeof (SettingsScope))]
  58. [JsonConverter (typeof (KeyJsonConverter))]
  59. public static Key QuitKey
  60. {
  61. get => _quitKey;
  62. set
  63. {
  64. if (_quitKey != value)
  65. {
  66. Key oldKey = _quitKey;
  67. _quitKey = value;
  68. OnQuitKeyChanged (new (oldKey, value));
  69. }
  70. }
  71. }
  72. private static void OnQuitKeyChanged (KeyChangedEventArgs e)
  73. {
  74. // TODO: The fact Top has it's own QuitKey and events is needlessly complex. Remove it.
  75. // Duplicate the list so if it changes during enumeration we're safe
  76. foreach (Toplevel top in _topLevels.ToArray ())
  77. {
  78. top.OnQuitKeyChanged (e);
  79. }
  80. }
  81. /// <summary>
  82. /// Event fired when the user presses a key. Fired by <see cref="OnKeyDown"/>.
  83. /// <para>
  84. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  85. /// additional processing.
  86. /// </para>
  87. /// </summary>
  88. /// <remarks>
  89. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
  90. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  91. /// <para>Fired after <see cref="KeyDown"/> and before <see cref="KeyUp"/>.</para>
  92. /// </remarks>
  93. public static event EventHandler<Key> KeyDown;
  94. /// <summary>
  95. /// Called by the <see cref="ConsoleDriver"/> when the user presses a key. Fires the <see cref="KeyDown"/> event
  96. /// then calls <see cref="View.NewKeyDownEvent"/> on all top level views. Called after <see cref="OnKeyDown"/> and
  97. /// before <see cref="OnKeyUp"/>.
  98. /// </summary>
  99. /// <remarks>Can be used to simulate key press events.</remarks>
  100. /// <param name="keyEvent"></param>
  101. /// <returns><see langword="true"/> if the key was handled.</returns>
  102. public static bool OnKeyDown (Key keyEvent)
  103. {
  104. if (!_initialized)
  105. {
  106. return true;
  107. }
  108. KeyDown?.Invoke (null, keyEvent);
  109. if (keyEvent.Handled)
  110. {
  111. return true;
  112. }
  113. foreach (Toplevel topLevel in _topLevels.ToList ())
  114. {
  115. if (topLevel.NewKeyDownEvent (keyEvent))
  116. {
  117. return true;
  118. }
  119. if (topLevel.Modal)
  120. {
  121. break;
  122. }
  123. }
  124. // Invoke any global (Application-scoped) KeyBindings.
  125. // The first view that handles the key will stop the loop.
  126. foreach (KeyValuePair<Key, List<View>> binding in _keyBindings.Where (b => b.Key == keyEvent.KeyCode))
  127. {
  128. foreach (View view in binding.Value)
  129. {
  130. if (view is {} && view.KeyBindings.TryGet (binding.Key, (KeyBindingScope)0xFFFF, out KeyBinding kb))
  131. {
  132. //bool? handled = view.InvokeCommands (kb.Commands, binding.Key, kb);
  133. bool? handled = view?.OnInvokingKeyBindings (keyEvent, kb.Scope);
  134. if (handled != null && (bool)handled)
  135. {
  136. return true;
  137. }
  138. }
  139. }
  140. }
  141. return false;
  142. }
  143. /// <summary>
  144. /// Event fired when the user releases a key. Fired by <see cref="OnKeyUp"/>.
  145. /// <para>
  146. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  147. /// additional processing.
  148. /// </para>
  149. /// </summary>
  150. /// <remarks>
  151. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
  152. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  153. /// <para>Fired after <see cref="KeyDown"/>.</para>
  154. /// </remarks>
  155. public static event EventHandler<Key> KeyUp;
  156. /// <summary>
  157. /// Called by the <see cref="ConsoleDriver"/> when the user releases a key. Fires the <see cref="KeyUp"/> event
  158. /// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="OnKeyDown"/>.
  159. /// </summary>
  160. /// <remarks>Can be used to simulate key press events.</remarks>
  161. /// <param name="a"></param>
  162. /// <returns><see langword="true"/> if the key was handled.</returns>
  163. public static bool OnKeyUp (Key a)
  164. {
  165. if (!_initialized)
  166. {
  167. return true;
  168. }
  169. KeyUp?.Invoke (null, a);
  170. if (a.Handled)
  171. {
  172. return true;
  173. }
  174. foreach (Toplevel topLevel in _topLevels.ToList ())
  175. {
  176. if (topLevel.NewKeyUpEvent (a))
  177. {
  178. return true;
  179. }
  180. if (topLevel.Modal)
  181. {
  182. break;
  183. }
  184. }
  185. return false;
  186. }
  187. /// <summary>
  188. /// The <see cref="KeyBindingScope.Application"/> key bindings.
  189. /// </summary>
  190. private static readonly Dictionary<Key, List<View>> _keyBindings = new ();
  191. /// <summary>
  192. /// Gets the list of <see cref="KeyBindingScope.Application"/> key bindings.
  193. /// </summary>
  194. public static Dictionary<Key, List<View>> GetKeyBindings () { return _keyBindings; }
  195. /// <summary>
  196. /// Adds an <see cref="KeyBindingScope.Application"/> scoped key binding.
  197. /// </summary>
  198. /// <remarks>
  199. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  200. /// </remarks>
  201. /// <param name="key">The key being bound.</param>
  202. /// <param name="view">The view that is bound to the key.</param>
  203. internal static void AddKeyBinding (Key key, View view)
  204. {
  205. if (!_keyBindings.ContainsKey (key))
  206. {
  207. _keyBindings [key] = [];
  208. }
  209. _keyBindings [key].Add (view);
  210. }
  211. /// <summary>
  212. /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings.
  213. /// </summary>
  214. /// <remarks>
  215. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  216. /// </remarks>
  217. /// <returns>The list of Views that have Application-scoped key bindings.</returns>
  218. internal static List<View> GetViewsWithKeyBindings () { return _keyBindings.Values.SelectMany (v => v).ToList (); }
  219. /// <summary>
  220. /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings for the specified key.
  221. /// </summary>
  222. /// <remarks>
  223. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  224. /// </remarks>
  225. /// <param name="key">The key to check.</param>
  226. /// <param name="views">Outputs the list of views bound to <paramref name="key"/></param>
  227. /// <returns><see langword="True"/> if successful.</returns>
  228. internal static bool TryGetKeyBindings (Key key, out List<View> views) { return _keyBindings.TryGetValue (key, out views); }
  229. /// <summary>
  230. /// Removes an <see cref="KeyBindingScope.Application"/> scoped key binding.
  231. /// </summary>
  232. /// <remarks>
  233. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  234. /// </remarks>
  235. /// <param name="key">The key that was bound.</param>
  236. /// <param name="view">The view that is bound to the key.</param>
  237. internal static void RemoveKeyBinding (Key key, View view)
  238. {
  239. if (_keyBindings.TryGetValue (key, out List<View> views))
  240. {
  241. views.Remove (view);
  242. if (views.Count == 0)
  243. {
  244. _keyBindings.Remove (key);
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
  250. /// </summary>
  251. /// <remarks>
  252. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  253. /// </remarks>
  254. /// <param name="view">The view that is bound to the key.</param>
  255. internal static void ClearKeyBindings (View view)
  256. {
  257. foreach (Key key in _keyBindings.Keys)
  258. {
  259. _keyBindings [key].Remove (view);
  260. }
  261. }
  262. /// <summary>
  263. /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
  264. /// </summary>
  265. /// <remarks>
  266. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  267. /// </remarks>
  268. internal static void ClearKeyBindings () { _keyBindings.Clear (); }
  269. }