KeyBindings.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Text;
  6. using Terminal.Gui;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("KeyBindings", "Illustrates the KeyBindings API.")]
  9. [ScenarioCategory ("Mouse and Keyboard")]
  10. public sealed class KeyBindings : Scenario
  11. {
  12. private readonly ObservableCollection<string> _focusedBindings = [];
  13. private ListView _focusedBindingsListView;
  14. public override void Main ()
  15. {
  16. // Init
  17. Application.Init ();
  18. // Setup - Create a top-level application window and configure it.
  19. Window appWindow = new ()
  20. {
  21. Title = GetQuitKeyAndName (),
  22. SuperViewRendersLineCanvas = true,
  23. };
  24. Label label = new ()
  25. {
  26. Title = "_Label:",
  27. };
  28. TextField textField = new ()
  29. {
  30. X = Pos.Right (label),
  31. Y = Pos.Top (label),
  32. Width = 20,
  33. };
  34. appWindow.Add (label, textField);
  35. Button button = new ()
  36. {
  37. X = Pos.Right (textField) + 1,
  38. Y = Pos.Top (label),
  39. Text = "_Button",
  40. };
  41. appWindow.Add (button);
  42. KeyBindingsDemo keyBindingsDemo = new ()
  43. {
  44. X = Pos.Right (button) + 1,
  45. Width = Dim.Auto (DimAutoStyle.Text),
  46. Height = Dim.Auto (DimAutoStyle.Text),
  47. HotKeySpecifier = (Rune)'_',
  48. Title = "_KeyBindingsDemo",
  49. Text = $"""
  50. These keys will cause this view to show a message box:
  51. - Hotkey: k, K, Alt-K, Alt-Shift-K
  52. - Focused: F3
  53. - Application: F4
  54. Pressing Esc or {Application.QuitKey} will cause it to quit the app.
  55. """,
  56. BorderStyle = LineStyle.Dashed
  57. };
  58. appWindow.Add (keyBindingsDemo);
  59. ObservableCollection<string> appBindings = new ();
  60. ListView appBindingsListView = new ()
  61. {
  62. Title = "_Application Bindings",
  63. BorderStyle = LineStyle.Single,
  64. X = -1,
  65. Y = Pos.Bottom (keyBindingsDemo) + 1,
  66. Width = Dim.Auto (),
  67. Height = Dim.Fill () + 1,
  68. CanFocus = true,
  69. Source = new ListWrapper<string> (appBindings),
  70. SuperViewRendersLineCanvas = true
  71. };
  72. appWindow.Add (appBindingsListView);
  73. foreach (var key in Application.KeyBindings.GetBoundKeys())
  74. {
  75. var binding = Application.KeyBindings.Get (key);
  76. appBindings.Add ($"{key} -> {binding.Target?.GetType ().Name} - {binding.Commands [0]}");
  77. }
  78. ObservableCollection<string> hotkeyBindings = new ();
  79. ListView hotkeyBindingsListView = new ()
  80. {
  81. Title = "_Hotkey Bindings",
  82. BorderStyle = LineStyle.Single,
  83. X = Pos.Right (appBindingsListView) - 1,
  84. Y = Pos.Bottom (keyBindingsDemo) + 1,
  85. Width = Dim.Auto (),
  86. Height = Dim.Fill () + 1,
  87. CanFocus = true,
  88. Source = new ListWrapper<string> (hotkeyBindings),
  89. SuperViewRendersLineCanvas = true
  90. };
  91. appWindow.Add (hotkeyBindingsListView);
  92. foreach (var subview in appWindow.Subviews)
  93. {
  94. foreach (var binding in subview.HotKeyBindings.Bindings)
  95. {
  96. hotkeyBindings.Add ($"{binding.Key} -> {subview.GetType ().Name} - {binding.Value.Commands [0]}");
  97. }
  98. }
  99. _focusedBindingsListView = new ()
  100. {
  101. Title = "_Focused Bindings",
  102. BorderStyle = LineStyle.Single,
  103. X = Pos.Right (hotkeyBindingsListView) - 1,
  104. Y = Pos.Bottom (keyBindingsDemo) + 1,
  105. Width = Dim.Auto (),
  106. Height = Dim.Fill () + 1,
  107. CanFocus = true,
  108. Source = new ListWrapper<string> (_focusedBindings),
  109. SuperViewRendersLineCanvas = true
  110. };
  111. appWindow.Add (_focusedBindingsListView);
  112. Application.Navigation!.FocusedChanged += Application_HasFocusChanged;
  113. // Run - Start the application.
  114. Application.Run (appWindow);
  115. Application.Navigation!.FocusedChanged -= Application_HasFocusChanged;
  116. appWindow.Dispose ();
  117. // Shutdown - Calling Application.Shutdown is required.
  118. Application.Shutdown ();
  119. }
  120. private void Application_HasFocusChanged (object sender, EventArgs e)
  121. {
  122. View focused = Application.Navigation!.GetFocused ();
  123. if (focused == null)
  124. {
  125. return;
  126. }
  127. _focusedBindingsListView.Title = $"_Focused ({focused?.GetType ().Name}) Bindings";
  128. _focusedBindings.Clear();
  129. foreach (var binding in focused?.KeyBindings!.Bindings)
  130. {
  131. _focusedBindings.Add ($"{binding.Key} -> {binding.Value.Commands [0]}");
  132. }
  133. }
  134. }
  135. public class KeyBindingsDemo : View
  136. {
  137. public KeyBindingsDemo ()
  138. {
  139. CanFocus = true;
  140. AddCommand (Command.Save, ctx =>
  141. {
  142. MessageBox.Query ($"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
  143. return true;
  144. });
  145. AddCommand (Command.New, ctx =>
  146. {
  147. MessageBox.Query ($"{ctx.Command}", $"Ctx: {ctx}", buttons: "Ok");
  148. return true;
  149. });
  150. AddCommand (Command.HotKey, ctx =>
  151. {
  152. MessageBox.Query ($"{ctx.Command}", $"Ctx: {ctx}\nCommand: {ctx.Command}", buttons: "Ok");
  153. SetFocus ();
  154. return true;
  155. });
  156. KeyBindings.Add (Key.F2, Command.Save);
  157. KeyBindings.Add (Key.F3, Command.New); // same as specifying KeyBindingScope.Focused
  158. Application.KeyBindings.Add (Key.F4, this, Command.New);
  159. AddCommand (Command.Quit, ctx =>
  160. {
  161. if (ctx is not CommandContext<KeyBinding> keyCommandContext)
  162. {
  163. return false;
  164. }
  165. MessageBox.Query ($"{keyCommandContext.Binding}", $"Key: {keyCommandContext.Binding.Key}\nCommand: {ctx.Command}", buttons: "Ok");
  166. Application.RequestStop ();
  167. return true;
  168. });
  169. Application.KeyBindings.Add (Key.Q.WithAlt, this, Command.Quit);
  170. }
  171. }