Application.Keyboard.cs 12 KB

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