View.Keyboard.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #nullable enable
  2. namespace Terminal.Gui.ViewBase;
  3. public partial class View // Keyboard APIs
  4. {
  5. /// <summary>
  6. /// Helper to configure all things keyboard related for a View. Called from the View constructor.
  7. /// </summary>
  8. private void SetupKeyboard ()
  9. {
  10. KeyBindings = new (this);
  11. KeyBindings.Add (Key.Space, Command.Select);
  12. KeyBindings.Add (Key.Enter, Command.Accept);
  13. HotKeyBindings = new (this);
  14. // Note, setting HotKey will bind HotKey to Command.HotKey
  15. HotKeySpecifier = (Rune)'_';
  16. TitleTextFormatter.HotKeyChanged += TitleTextFormatter_HotKeyChanged;
  17. }
  18. /// <summary>
  19. /// Helper to dispose all things keyboard related for a View. Called from the View Dispose method.
  20. /// </summary>
  21. private void DisposeKeyboard () { TitleTextFormatter.HotKeyChanged -= TitleTextFormatter_HotKeyChanged; }
  22. #region HotKey Support
  23. /// <summary>Raised when the <see cref="HotKey"/> is changed.</summary>
  24. public event EventHandler<KeyChangedEventArgs>? HotKeyChanged;
  25. private Key _hotKey = new ();
  26. private void TitleTextFormatter_HotKeyChanged (object? sender, KeyChangedEventArgs e) { HotKeyChanged?.Invoke (this, e); }
  27. /// <summary>
  28. /// Gets or sets the hot key defined for this view. Pressing the hot key on the keyboard while this view has focus will
  29. /// invoke <see cref="Command.HotKey"/>. By default, the HotKey is set to the first character of <see cref="Text"/>
  30. /// that is prefixed with <see cref="HotKeySpecifier"/>.
  31. /// <para>
  32. /// A HotKey is a keypress that causes a visible UI item to perform an action. For example, in a Dialog,
  33. /// with a Button with the text of "_Text" <c>Alt+T</c> will cause the button to gain focus and to raise its
  34. /// <see cref="Accepting"/> event.
  35. /// Or, in a
  36. /// <see cref="Menu"/> with "_File _Edit", <c>Alt+F</c> will select (show) the "_File" menu. If the "_File" menu
  37. /// has a
  38. /// sub-menu of "_New" <c>Alt+N</c> or <c>N</c> will ONLY select the "_New" sub-menu if the "_File" menu is already
  39. /// opened.
  40. /// </para>
  41. /// <para>
  42. /// View subclasses can use <see cref="View.AddCommand(Command,CommandImplementation)"/> to
  43. /// define the
  44. /// behavior of the hot key.
  45. /// </para>
  46. /// </summary>
  47. /// <remarks>
  48. /// <para>See <see href="../docs/keyboard.md"/> for an overview of Terminal.Gui keyboard APIs.</para>
  49. /// <para>
  50. /// This is a helper API for configuring a key binding for the hot key. By default, this property is set whenever
  51. /// <see cref="Text"/> changes.
  52. /// </para>
  53. /// <para>
  54. /// By default, when the Hot Key is set, key bindings are added for both the base key (e.g.
  55. /// <see cref="Key.D3"/>) and the Alt-shifted key (e.g. <see cref="Key.D3"/>.
  56. /// <see cref="Key.WithAlt"/>). This behavior can be overriden by overriding
  57. /// <see cref="AddKeyBindingsForHotKey"/>.
  58. /// </para>
  59. /// <para>
  60. /// By default, when the HotKey is set to <see cref="Key.A"/> through <see cref="Key.Z"/> key bindings will
  61. /// be added for both the un-shifted and shifted versions. This means if the HotKey is <see cref="Key.A"/>, key
  62. /// bindings for <c>Key.A</c> and <c>Key.A.WithShift</c> will be added. This behavior can be overriden by
  63. /// overriding <see cref="AddKeyBindingsForHotKey"/>.
  64. /// </para>
  65. /// <para>If the hot key is changed, the <see cref="HotKeyChanged"/> event is fired.</para>
  66. /// <para>Set to <see cref="Key.Empty"/> to disable the hot key.</para>
  67. /// </remarks>
  68. public Key HotKey
  69. {
  70. get => _hotKey;
  71. set
  72. {
  73. if (value is null)
  74. {
  75. throw new ArgumentException (
  76. @"HotKey must not be null. Use Key.Empty to clear the HotKey.",
  77. nameof (value)
  78. );
  79. }
  80. if (AddKeyBindingsForHotKey (_hotKey, value))
  81. {
  82. // This will cause TextFormatter_HotKeyChanged to be called, firing HotKeyChanged
  83. // BUGBUG: _hotkey should be set BEFORE setting TextFormatter.HotKey
  84. _hotKey = value;
  85. TitleTextFormatter.HotKey = value;
  86. }
  87. }
  88. }
  89. /// <summary>
  90. /// Adds key bindings for the specified HotKey. Useful for views that contain multiple items that each have their
  91. /// own HotKey such as <see cref="OptionSelector"/>.
  92. /// </summary>
  93. /// <remarks>
  94. /// <para>
  95. /// By default, key bindings are added for both the base key (e.g. <see cref="Key.D3"/>) and the Alt-shifted key
  96. /// (e.g. <c>Key.D3.WithAlt</c>) This behavior can be overriden by overriding <see cref="AddKeyBindingsForHotKey"/>
  97. /// .
  98. /// </para>
  99. /// <para>
  100. /// By default, when <paramref name="hotKey"/> is <see cref="Key.A"/> through <see cref="Key.Z"/> key bindings
  101. /// will be added for both the un-shifted and shifted versions. This means if the HotKey is <see cref="Key.A"/>,
  102. /// key bindings for <c>Key.A</c> and <c>Key.A.WithShift</c> will be added. This behavior can be overriden by
  103. /// overriding <see cref="AddKeyBindingsForHotKey"/>.
  104. /// </para>
  105. /// </remarks>
  106. /// <param name="prevHotKey">The HotKey <paramref name="hotKey"/> is replacing. Key bindings for this key will be removed.</param>
  107. /// <param name="hotKey">The new HotKey. If <see cref="Key.Empty"/> <paramref name="prevHotKey"/> bindings will be removed.</param>
  108. /// <param name="context">Arbitrary context that can be associated with this key binding.</param>
  109. /// <returns><see langword="true"/> if the HotKey bindings were added.</returns>
  110. /// <exception cref="ArgumentException"></exception>
  111. public virtual bool AddKeyBindingsForHotKey (Key prevHotKey, Key hotKey, object? context = null)
  112. {
  113. if (_hotKey == hotKey)
  114. {
  115. return false;
  116. }
  117. Key newKey = hotKey;
  118. Key baseKey = newKey.NoAlt.NoShift.NoCtrl;
  119. if (newKey != Key.Empty && (baseKey == Key.Space || Rune.IsControl (baseKey.AsRune)))
  120. {
  121. throw new ArgumentException (@$"HotKey must be a printable (and non-space) key ({hotKey}).");
  122. }
  123. if (newKey != baseKey)
  124. {
  125. if (newKey.IsCtrl)
  126. {
  127. throw new ArgumentException (@$"HotKey does not support CtrlMask ({hotKey}).");
  128. }
  129. // Strip off the shift mask if it's A...Z
  130. if (baseKey.IsKeyCodeAtoZ)
  131. {
  132. newKey = newKey.NoShift;
  133. }
  134. // Strip off the Alt mask
  135. newKey = newKey.NoAlt;
  136. }
  137. // Remove base version
  138. if (HotKeyBindings.TryGet (prevHotKey, out _))
  139. {
  140. HotKeyBindings.Remove (prevHotKey);
  141. }
  142. // Remove the Alt version
  143. if (HotKeyBindings.TryGet (prevHotKey.WithAlt, out _))
  144. {
  145. HotKeyBindings.Remove (prevHotKey.WithAlt);
  146. }
  147. if (_hotKey.IsKeyCodeAtoZ)
  148. {
  149. // Remove the shift version
  150. if (HotKeyBindings.TryGet (prevHotKey.WithShift, out _))
  151. {
  152. HotKeyBindings.Remove (prevHotKey.WithShift);
  153. }
  154. // Remove alt | shift version
  155. if (HotKeyBindings.TryGet (prevHotKey.WithShift.WithAlt, out _))
  156. {
  157. HotKeyBindings.Remove (prevHotKey.WithShift.WithAlt);
  158. }
  159. }
  160. // Add the new
  161. if (newKey != Key.Empty)
  162. {
  163. KeyBinding keyBinding = new ()
  164. {
  165. Commands = [Command.HotKey],
  166. Key = newKey,
  167. Data = context
  168. };
  169. // Add the base and Alt key
  170. HotKeyBindings.Remove (newKey);
  171. HotKeyBindings.Add (newKey, keyBinding);
  172. HotKeyBindings.Remove (newKey.WithAlt);
  173. HotKeyBindings.Add (newKey.WithAlt, keyBinding);
  174. // If the Key is A..Z, add ShiftMask and AltMask | ShiftMask
  175. if (newKey.IsKeyCodeAtoZ)
  176. {
  177. HotKeyBindings.Remove (newKey.WithShift);
  178. HotKeyBindings.Add (newKey.WithShift, keyBinding);
  179. HotKeyBindings.Remove (newKey.WithShift.WithAlt);
  180. HotKeyBindings.Add (newKey.WithShift.WithAlt, keyBinding);
  181. }
  182. }
  183. return true;
  184. }
  185. /// <summary>
  186. /// Gets or sets the specifier character for the hot key (e.g. '_'). Set to '\xffff' to disable automatic hot key
  187. /// setting support for this View instance. The default is '\xffff'.
  188. /// </summary>
  189. public virtual Rune HotKeySpecifier
  190. {
  191. get => TitleTextFormatter.HotKeySpecifier;
  192. set
  193. {
  194. TitleTextFormatter.HotKeySpecifier = TextFormatter.HotKeySpecifier = value;
  195. SetHotKeyFromTitle ();
  196. }
  197. }
  198. private void SetHotKeyFromTitle ()
  199. {
  200. if (HotKeySpecifier == new Rune ('\xFFFF'))
  201. {
  202. return; // throw new InvalidOperationException ("Can't set HotKey unless a TextFormatter has been created");
  203. }
  204. if (Terminal.Gui.Text.TextFormatter.FindHotKey (_title, HotKeySpecifier, out _, out Key hk))
  205. {
  206. if (_hotKey != hk)
  207. {
  208. HotKey = hk;
  209. }
  210. }
  211. else
  212. {
  213. HotKey = Key.Empty;
  214. }
  215. }
  216. #endregion HotKey Support
  217. #region Low-level Key handling
  218. #region Key Down Event
  219. /// <summary>
  220. /// If the view is enabled, raises the related key down events on the view, and returns <see langword="true"/> if the
  221. /// event was
  222. /// handled.
  223. /// </summary>
  224. /// <remarks>
  225. /// <para>
  226. /// If the view has a sub view that is focused, <see cref="NewKeyDownEvent"/> will be called on the focused view
  227. /// first.
  228. /// </para>
  229. /// <para>
  230. /// If a more focused subview does not handle the key press, this method raises <see cref="OnKeyDown"/>/
  231. /// <see cref="KeyDown"/> to allow the
  232. /// view to pre-process the key press. If <see cref="OnKeyDown"/>/<see cref="KeyDown"/> is not handled any commands
  233. /// bound to the key will be invoked.
  234. /// Then, only if no key bindings are
  235. /// handled, <see cref="OnKeyDownNotHandled"/>/<see cref="KeyDownNotHandled"/> will be raised allowing the view to
  236. /// process the key press.
  237. /// </para>
  238. /// <para>
  239. /// Calling this method for a key bound to the view via an Application-scoped keybinding will have no effect.
  240. /// Instead,
  241. /// use <see cref="Application.RaiseKeyDownEvent"/>.
  242. /// </para>
  243. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  244. /// </remarks>
  245. /// <param name="key"></param>
  246. /// <returns><see langword="true"/> if the event was handled.</returns>
  247. public bool NewKeyDownEvent (Key key)
  248. {
  249. if (!Enabled)
  250. {
  251. return false;
  252. }
  253. // TODO: We really need an event before recursing into Focused. Without, there's no way for
  254. // TODO: SuperViews to prevent SubViews from seeing certain keys. A use-case for this:
  255. // TODO: - MenuBar needs to prevent MenuItems from seeing QuitKey if the MenuItem is not visible
  256. // If there's a Focused subview, give it a chance (this recurses down the hierarchy)
  257. if (Focused?.NewKeyDownEvent (key) == true)
  258. {
  259. return true;
  260. }
  261. // Before (fire the cancellable event)
  262. if (RaiseKeyDown (key) || key.Handled)
  263. {
  264. return true;
  265. }
  266. // During (this is what can be cancelled)
  267. // TODO: NewKeyDownEvent returns bool. It should be bool? so state of InvokeCommands can be reflected up stack
  268. if (InvokeCommands (key) is true || key.Handled)
  269. {
  270. return true;
  271. }
  272. bool? handled = InvokeCommandsBoundToHotKey (key);
  273. if (handled is true)
  274. {
  275. return true;
  276. }
  277. // After
  278. if (RaiseKeyDownNotHandled (key) || key.Handled)
  279. {
  280. return true;
  281. }
  282. return key.Handled;
  283. bool RaiseKeyDown (Key k)
  284. {
  285. // Before (fire the cancellable event)
  286. if (OnKeyDown (k) || k.Handled)
  287. {
  288. return true;
  289. }
  290. // fire event
  291. KeyDown?.Invoke (this, k);
  292. return k.Handled;
  293. }
  294. bool RaiseKeyDownNotHandled (Key k)
  295. {
  296. if (OnKeyDownNotHandled (k) || k.Handled)
  297. {
  298. return true;
  299. }
  300. KeyDownNotHandled?.Invoke (this, k);
  301. return false;
  302. }
  303. }
  304. /// <summary>
  305. /// Called when the user presses a key, allowing subscribers to pre-process the key down event. Called
  306. /// before key bindings are invoked and <see cref="KeyDownNotHandled"/> is raised. Set
  307. /// <see cref="Key.Handled"/>
  308. /// to true to
  309. /// stop the key from being processed further.
  310. /// </summary>
  311. /// <param name="key">The key that produced the event.</param>
  312. /// <returns>
  313. /// <see langword="false"/> if the key down event was not handled. <see langword="true"/> if the event was handled
  314. /// and processing should stop.
  315. /// </returns>
  316. /// <remarks>
  317. /// <para>Fires the <see cref="KeyDown"/> event.</para>
  318. /// </remarks>
  319. protected virtual bool OnKeyDown (Key key) { return false; }
  320. /// <summary>
  321. /// Raised when the user presses a key, allowing subscribers to pre-process the key down event. Called
  322. /// before key bindings are invoked and <see cref="KeyDownNotHandled"/> is raised. Set
  323. /// <see cref="Key.Handled"/>
  324. /// to true to
  325. /// stop the key from being processed further.
  326. /// </summary>
  327. /// <remarks>
  328. /// <para>
  329. /// Not all terminals support key distinct up notifications, Applications should avoid depending on distinct
  330. /// KeyUp events.
  331. /// </para>
  332. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  333. /// </remarks>
  334. public event EventHandler<Key>? KeyDown;
  335. /// <summary>
  336. /// Called when the user has pressed key it wasn't handled by <see cref="KeyDown"/> and was not bound to a key binding.
  337. /// </summary>
  338. /// <remarks>
  339. /// <para>
  340. /// Not all terminals support distinct key up notifications; applications should avoid depending on distinct
  341. /// KeyUp events.
  342. /// </para>
  343. /// </remarks>
  344. /// <param name="key">Contains the details about the key that produced the event.</param>
  345. /// <returns>
  346. /// <see langword="false"/> if the key press was not handled. <see langword="true"/> if the keypress was handled
  347. /// and no other view should see it.
  348. /// </returns>
  349. protected virtual bool OnKeyDownNotHandled (Key key) { return key.Handled; }
  350. /// <summary>
  351. /// Raised when the user has pressed key it wasn't handled by <see cref="KeyDown"/> and was not bound to a key binding.
  352. /// </summary>
  353. /// <remarks>
  354. /// <para>
  355. /// SubViews can use the <see cref="KeyDownNotHandled"/> of their super view override the default behavior of when
  356. /// key bindings are invoked.
  357. /// </para>
  358. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  359. /// </remarks>
  360. public event EventHandler<Key>? KeyDownNotHandled;
  361. #endregion KeyDown Event
  362. #region KeyUp Event
  363. /// <summary>
  364. /// If the view is enabled, raises the related key up events on the view, and returns <see langword="true"/> if the
  365. /// event was
  366. /// handled.
  367. /// </summary>
  368. /// <remarks>
  369. /// <para>
  370. /// Not all terminals support key distinct down/up notifications, Applications should avoid depending on distinct
  371. /// KeyUp events.
  372. /// </para>
  373. /// <para>
  374. /// If the view has a sub view that is focused, <see cref="NewKeyUpEvent"/> will be called on the focused view
  375. /// first.
  376. /// </para>
  377. /// <para>
  378. /// If the focused sub view does not handle the key press, this method raises <see cref="OnKeyUp"/>/
  379. /// <see cref="KeyUp"/> to allow the
  380. /// view to pre-process the key press.
  381. /// </para>
  382. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  383. /// </remarks>
  384. /// <param name="key"></param>
  385. /// <returns><see langword="true"/> if the event was handled.</returns>
  386. public bool NewKeyUpEvent (Key key)
  387. {
  388. if (!Enabled)
  389. {
  390. return false;
  391. }
  392. // Before
  393. if (RaiseKeyUp (key) || key.Handled)
  394. {
  395. return true;
  396. }
  397. // During
  398. // After
  399. return false;
  400. bool RaiseKeyUp (Key k)
  401. {
  402. // Before (fire the cancellable event)
  403. if (OnKeyUp (k) || k.Handled)
  404. {
  405. return true;
  406. }
  407. // fire event
  408. KeyUp?.Invoke (this, k);
  409. return k.Handled;
  410. }
  411. }
  412. /// <summary>Called when a key is released. This method is called from <see cref="NewKeyUpEvent"/>.</summary>
  413. /// <param name="key">Contains the details about the key that produced the event.</param>
  414. /// <returns>
  415. /// <see langword="false"/> if the keys up event was not handled. <see langword="true"/> if no other view should see
  416. /// it.
  417. /// </returns>
  418. /// <remarks>
  419. /// Not all terminals support key distinct down/up notifications, Applications should avoid depending on distinct KeyUp
  420. /// events.
  421. /// <para>
  422. /// Overrides must call into the base and return <see langword="true"/> if the base returns
  423. /// <see langword="true"/>.
  424. /// </para>
  425. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  426. /// </remarks>
  427. public virtual bool OnKeyUp (Key key) { return false; }
  428. /// <summary>
  429. /// Raised when a key is released. Set <see cref="Key.Handled"/> to true to stop the key up event from being processed
  430. /// by other views.
  431. /// <remarks>
  432. /// Not all terminals support key distinct down/up notifications, Applications should avoid depending on
  433. /// distinct KeyDown and KeyUp events and instead should use <see cref="KeyDown"/>.
  434. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  435. /// </remarks>
  436. /// </summary>
  437. public event EventHandler<Key>? KeyUp;
  438. #endregion KeyUp Event
  439. #endregion Low-level Key handling
  440. #region Key Bindings
  441. /// <summary>Gets the bindings for this view that will be invoked only if this view has focus.</summary>
  442. public KeyBindings KeyBindings { get; internal set; } = null!;
  443. /// <summary>Gets the bindings for this view that will be invoked regardless of whether this view has focus or not.</summary>
  444. public KeyBindings HotKeyBindings { get; internal set; } = null!;
  445. /// <summary>
  446. /// INTERNAL API: Invokes any commands bound to <paramref name="key"/> on this view, adornments, and subviews.
  447. /// </summary>
  448. /// <param name="key"></param>
  449. /// <returns>
  450. /// <see langword="null"/> if no command was invoked or there was no matching key binding; input processing should
  451. /// continue.
  452. /// <see langword="false"/> if a command was invoked and was not handled (or cancelled); input processing should
  453. /// continue.
  454. /// <see langword="true"/> if at least one command was invoked and handled (or
  455. /// cancelled); input processing should stop.
  456. /// </returns>
  457. internal bool? InvokeCommands (Key key)
  458. {
  459. // * If no key binding was found, `InvokeKeyBindings` returns `null`.
  460. // Continue passing the event (return `false` from `OnInvokeKeyBindings`).
  461. // * If key bindings were found, but none handled the key (all `Command`s returned `false`),
  462. // `InvokeKeyBindings` returns `false`. Continue passing the event (return `false` from `OnInvokeKeyBindings`)..
  463. // * If key bindings were found, and any handled the key (at least one `Command` returned `true`),
  464. // `InvokeKeyBindings` returns `true`. Continue passing the event (return `false` from `OnInvokeKeyBindings`).
  465. bool? handled = DoInvokeCommands (key);
  466. if (handled is true)
  467. {
  468. // Stop processing if any key binding handled the key.
  469. // DO NOT stop processing if there are no matching key bindings or none of the key bindings handled the key
  470. return handled;
  471. }
  472. if (Margin is { } && InvokeCommandsBoundToKeyOnAdornment (Margin, key, ref handled))
  473. {
  474. return true;
  475. }
  476. if (Padding is { } && InvokeCommandsBoundToKeyOnAdornment (Padding, key, ref handled))
  477. {
  478. return true;
  479. }
  480. if (Border is { } && InvokeCommandsBoundToKeyOnAdornment (Border, key, ref handled))
  481. {
  482. return true;
  483. }
  484. return handled;
  485. }
  486. private static bool InvokeCommandsBoundToKeyOnAdornment (Adornment adornment, Key key, ref bool? handled)
  487. {
  488. if (!adornment.Enabled)
  489. {
  490. return false;
  491. }
  492. bool? adornmentHandled = adornment.InvokeCommands (key);
  493. if (adornmentHandled is true)
  494. {
  495. return true;
  496. }
  497. if (adornment?.InternalSubViews is null)
  498. {
  499. return false;
  500. }
  501. foreach (View subview in adornment.InternalSubViews)
  502. {
  503. bool? subViewHandled = subview.InvokeCommands (key);
  504. if (subViewHandled is { })
  505. {
  506. handled = subViewHandled;
  507. if ((bool)subViewHandled)
  508. {
  509. return true;
  510. }
  511. }
  512. }
  513. return false;
  514. }
  515. // BUGBUG: This will miss any hotkeys in subviews of Adornments.
  516. /// <summary>
  517. /// Invokes any commands bound to <paramref name="hotKey"/> on this view and subviews.
  518. /// </summary>
  519. /// <param name="hotKey"></param>
  520. /// <returns>
  521. /// <see langword="null"/> if no command was invoked; input processing should continue.
  522. /// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
  523. /// should continue.
  524. /// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
  525. /// stop.
  526. /// </returns>
  527. internal bool? InvokeCommandsBoundToHotKey (Key hotKey)
  528. {
  529. if (!Enabled)
  530. {
  531. return false;
  532. }
  533. bool? handled = null;
  534. // Process this View
  535. if (HotKeyBindings.TryGet (hotKey, out KeyBinding binding))
  536. {
  537. if (binding.Key is null || binding.Key == Key.Empty)
  538. {
  539. binding.Key = hotKey;
  540. }
  541. if (InvokeCommands (binding.Commands, binding) is true)
  542. {
  543. return true;
  544. }
  545. }
  546. // Now, process any HotKey bindings in the subviews
  547. foreach (View subview in InternalSubViews.ToList())
  548. {
  549. if (subview == Focused)
  550. {
  551. continue;
  552. }
  553. bool? recurse = subview.InvokeCommandsBoundToHotKey (hotKey);
  554. if (recurse is true || (handled is { } && (bool)handled))
  555. {
  556. return true;
  557. }
  558. }
  559. return false;
  560. }
  561. /// <summary>
  562. /// Invokes the Commands bound to <paramref name="key"/>.
  563. /// <para>See <see href="../docs/keyboard.md">for an overview of Terminal.Gui keyboard APIs.</see></para>
  564. /// </summary>
  565. /// <param name="key">The key event passed.</param>
  566. /// <returns>
  567. /// <see langword="null"/> if no command was invoked; input processing should continue.
  568. /// <see langword="false"/> if at least one command was invoked and was not handled (or cancelled); input processing
  569. /// should continue.
  570. /// <see langword="true"/> if at least one command was invoked and handled (or cancelled); input processing should
  571. /// stop.
  572. /// </returns>
  573. protected bool? DoInvokeCommands (Key key)
  574. {
  575. if (!KeyBindings.TryGet (key, out KeyBinding binding))
  576. {
  577. return null;
  578. }
  579. // TODO: Should we set binding.Key = key if it's not set?
  580. if (binding is {} && (binding.Key is null || !binding.Key.IsValid))
  581. {
  582. binding.Key = key;
  583. }
  584. return InvokeCommands (binding.Commands, binding);
  585. }
  586. #endregion Key Bindings
  587. }