ApplicationKeyboard.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. if (view is {} && view.KeyBindings.TryGet (binding.Key, (KeyBindingScope)0xFFFF, out KeyBinding kb))
  128. {
  129. //bool? handled = view.InvokeCommands (kb.Commands, binding.Key, kb);
  130. bool? handled = view?.OnInvokingKeyBindings (keyEvent, kb.Scope);
  131. if (handled != null && (bool)handled)
  132. {
  133. return true;
  134. }
  135. }
  136. }
  137. }
  138. return false;
  139. }
  140. /// <summary>
  141. /// Event fired when the user releases a key. Fired by <see cref="OnKeyUp"/>.
  142. /// <para>
  143. /// Set <see cref="Key.Handled"/> to <see langword="true"/> to indicate the key was handled and to prevent
  144. /// additional processing.
  145. /// </para>
  146. /// </summary>
  147. /// <remarks>
  148. /// All drivers support firing the <see cref="KeyDown"/> event. Some drivers (Curses) do not support firing the
  149. /// <see cref="KeyDown"/> and <see cref="KeyUp"/> events.
  150. /// <para>Fired after <see cref="KeyDown"/>.</para>
  151. /// </remarks>
  152. public static event EventHandler<Key> KeyUp;
  153. /// <summary>
  154. /// Called by the <see cref="ConsoleDriver"/> when the user releases a key. Fires the <see cref="KeyUp"/> event
  155. /// then calls <see cref="View.NewKeyUpEvent"/> on all top level views. Called after <see cref="OnKeyDown"/>.
  156. /// </summary>
  157. /// <remarks>Can be used to simulate key press events.</remarks>
  158. /// <param name="a"></param>
  159. /// <returns><see langword="true"/> if the key was handled.</returns>
  160. public static bool OnKeyUp (Key a)
  161. {
  162. if (!_initialized)
  163. {
  164. return true;
  165. }
  166. KeyUp?.Invoke (null, a);
  167. if (a.Handled)
  168. {
  169. return true;
  170. }
  171. foreach (Toplevel topLevel in _topLevels.ToList ())
  172. {
  173. if (topLevel.NewKeyUpEvent (a))
  174. {
  175. return true;
  176. }
  177. if (topLevel.Modal)
  178. {
  179. break;
  180. }
  181. }
  182. return false;
  183. }
  184. /// <summary>
  185. /// The <see cref="KeyBindingScope.Application"/> key bindings.
  186. /// </summary>
  187. private static readonly Dictionary<Key, List<View>> _keyBindings = new ();
  188. /// <summary>
  189. /// Gets the list of <see cref="KeyBindingScope.Application"/> key bindings.
  190. /// </summary>
  191. public static Dictionary<Key, List<View>> GetKeyBindings () { return _keyBindings; }
  192. /// <summary>
  193. /// Adds an <see cref="KeyBindingScope.Application"/> scoped key binding.
  194. /// </summary>
  195. /// <remarks>
  196. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  197. /// </remarks>
  198. /// <param name="key">The key being bound.</param>
  199. /// <param name="view">The view that is bound to the key.</param>
  200. internal static void AddKeyBinding (Key key, View view)
  201. {
  202. if (!_keyBindings.ContainsKey (key))
  203. {
  204. _keyBindings [key] = [];
  205. }
  206. _keyBindings [key].Add (view);
  207. }
  208. /// <summary>
  209. /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings.
  210. /// </summary>
  211. /// <remarks>
  212. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  213. /// </remarks>
  214. /// <returns>The list of Views that have Application-scoped key bindings.</returns>
  215. internal static List<View> GetViewsWithKeyBindings () { return _keyBindings.Values.SelectMany (v => v).ToList (); }
  216. /// <summary>
  217. /// Gets the list of Views that have <see cref="KeyBindingScope.Application"/> key bindings for the specified key.
  218. /// </summary>
  219. /// <remarks>
  220. /// This is an internal method used by the <see cref="View"/> class to add Application key bindings.
  221. /// </remarks>
  222. /// <param name="key">The key to check.</param>
  223. /// <param name="views">Outputs the list of views bound to <paramref name="key"/></param>
  224. /// <returns><see langword="True"/> if successful.</returns>
  225. internal static bool TryGetKeyBindings (Key key, out List<View> views) { return _keyBindings.TryGetValue (key, out views); }
  226. /// <summary>
  227. /// Removes an <see cref="KeyBindingScope.Application"/> scoped key binding.
  228. /// </summary>
  229. /// <remarks>
  230. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  231. /// </remarks>
  232. /// <param name="key">The key that was bound.</param>
  233. /// <param name="view">The view that is bound to the key.</param>
  234. internal static void RemoveKeyBinding (Key key, View view)
  235. {
  236. if (_keyBindings.TryGetValue (key, out List<View> views))
  237. {
  238. views.Remove (view);
  239. if (views.Count == 0)
  240. {
  241. _keyBindings.Remove (key);
  242. }
  243. }
  244. }
  245. /// <summary>
  246. /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
  247. /// </summary>
  248. /// <remarks>
  249. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  250. /// </remarks>
  251. /// <param name="view">The view that is bound to the key.</param>
  252. internal static void ClearKeyBindings (View view)
  253. {
  254. foreach (Key key in _keyBindings.Keys)
  255. {
  256. _keyBindings [key].Remove (view);
  257. }
  258. }
  259. /// <summary>
  260. /// Removes all <see cref="KeyBindingScope.Application"/> scoped key bindings for the specified view.
  261. /// </summary>
  262. /// <remarks>
  263. /// This is an internal method used by the <see cref="View"/> class to remove Application key bindings.
  264. /// </remarks>
  265. internal static void ClearKeyBindings () { _keyBindings.Clear (); }
  266. }