ApplicationKeyboard.cs 10 KB

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