Keys.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. using NStack;
  2. using System.Collections.Generic;
  3. using Terminal.Gui;
  4. using Terminal.Gui.Configuration;
  5. namespace UICatalog.Scenarios {
  6. [ScenarioMetadata (Name: "Keys", Description: "Shows how to handle keyboard input")]
  7. [ScenarioCategory ("Mouse and Keyboard")]
  8. public class Keys : Scenario {
  9. class TestWindow : Window {
  10. public List<string> _processKeyList = new List<string> ();
  11. public List<string> _processHotKeyList = new List<string> ();
  12. public List<string> _processColdKeyList = new List<string> ();
  13. public TestWindow (ustring title = null) : base (title)
  14. {
  15. }
  16. public TestWindow (Rect frame, ustring title = null) : base (frame, title)
  17. {
  18. }
  19. public TestWindow (ustring title = null, int padding = 0) : base (title, padding)
  20. {
  21. }
  22. public TestWindow (Rect frame, ustring title = null, int padding = 0) : base (frame, title, padding)
  23. {
  24. }
  25. public override bool ProcessKey (KeyEvent keyEvent)
  26. {
  27. _processKeyList.Add (keyEvent.ToString ());
  28. return base.ProcessKey (keyEvent);
  29. }
  30. public override bool ProcessHotKey (KeyEvent keyEvent)
  31. {
  32. _processHotKeyList.Add (keyEvent.ToString ());
  33. return base.ProcessHotKey (keyEvent);
  34. }
  35. public override bool ProcessColdKey (KeyEvent keyEvent)
  36. {
  37. _processColdKeyList.Add (keyEvent.ToString ());
  38. return base.ProcessColdKey (keyEvent);
  39. }
  40. }
  41. public override void Init ()
  42. {
  43. Application.Init ();
  44. ConfigurationManager.Themes.Theme = Theme;
  45. ConfigurationManager.Apply ();
  46. Win = new TestWindow ($"{Application.QuitKey} to Quit - Scenario: {GetName ()}") {
  47. X = 0,
  48. Y = 0,
  49. Width = Dim.Fill (),
  50. Height = Dim.Fill (),
  51. ColorScheme = Colors.ColorSchemes [TopLevelColorScheme],
  52. };
  53. Application.Top.Add (Win);
  54. }
  55. public override void Setup ()
  56. {
  57. // Type text here: ______
  58. var editLabel = new Label ("Type text here:") {
  59. X = 0,
  60. Y = 0,
  61. };
  62. Win.Add (editLabel);
  63. var edit = new TextField ("") {
  64. X = Pos.Right (editLabel) + 1,
  65. Y = Pos.Top (editLabel),
  66. Width = Dim.Fill (2),
  67. };
  68. Win.Add (edit);
  69. // Last KeyPress: ______
  70. var keyPressedLabel = new Label ("Last KeyPress:") {
  71. X = Pos.Left (editLabel),
  72. Y = Pos.Top (editLabel) + 2,
  73. };
  74. Win.Add (keyPressedLabel);
  75. var labelKeypress = new Label ("") {
  76. X = Pos.Left (edit),
  77. Y = Pos.Top (keyPressedLabel),
  78. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  79. ColorScheme = Colors.Error,
  80. AutoSize = true
  81. };
  82. Win.Add (labelKeypress);
  83. Win.KeyPress += (a) => labelKeypress.Text = a.KeyEvent.ToString ();
  84. // Key stroke log:
  85. var keyLogLabel = new Label ("Key stroke log:") {
  86. X = Pos.Left (editLabel),
  87. Y = Pos.Top (editLabel) + 4,
  88. };
  89. Win.Add (keyLogLabel);
  90. var fakeKeyPress = new KeyEvent (Key.CtrlMask | Key.A, new KeyModifiers () {
  91. Alt = true,
  92. Ctrl = true,
  93. Shift = true
  94. });
  95. var maxLogEntry = $"Key{"",-5}: {fakeKeyPress}".Length;
  96. var yOffset = (Application.Top == Application.Top ? 1 : 6);
  97. var keyStrokelist = new List<string> ();
  98. var keyStrokeListView = new ListView (keyStrokelist) {
  99. X = 0,
  100. Y = Pos.Top (keyLogLabel) + yOffset,
  101. Width = Dim.Percent (30),
  102. Height = Dim.Fill (),
  103. };
  104. keyStrokeListView.ColorScheme = Colors.TopLevel;
  105. Win.Add (keyStrokeListView);
  106. // ProcessKey log:
  107. var processKeyLogLabel = new Label ("ProcessKey log:") {
  108. X = Pos.Right (keyStrokeListView) + 1,
  109. Y = Pos.Top (editLabel) + 4,
  110. };
  111. Win.Add (processKeyLogLabel);
  112. maxLogEntry = $"{fakeKeyPress}".Length;
  113. yOffset = (Application.Top == Application.Top ? 1 : 6);
  114. var processKeyListView = new ListView (((TestWindow)Win)._processKeyList) {
  115. X = Pos.Left (processKeyLogLabel),
  116. Y = Pos.Top (processKeyLogLabel) + yOffset,
  117. Width = Dim.Percent(30),
  118. Height = Dim.Fill (),
  119. };
  120. processKeyListView.ColorScheme = Colors.TopLevel;
  121. Win.Add (processKeyListView);
  122. // ProcessHotKey log:
  123. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  124. var processHotKeyLogLabel = new Label ("ProcessHotKey log:") {
  125. X = Pos.Right (processKeyListView) + 1,
  126. Y = Pos.Top (editLabel) + 4,
  127. };
  128. Win.Add (processHotKeyLogLabel);
  129. yOffset = (Application.Top == Application.Top ? 1 : 6);
  130. var processHotKeyListView = new ListView (((TestWindow)Win)._processHotKeyList) {
  131. X = Pos.Left (processHotKeyLogLabel),
  132. Y = Pos.Top (processHotKeyLogLabel) + yOffset,
  133. Width = Dim.Percent (20),
  134. Height = Dim.Fill (),
  135. };
  136. processHotKeyListView.ColorScheme = Colors.TopLevel;
  137. Win.Add (processHotKeyListView);
  138. // ProcessColdKey log:
  139. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  140. var processColdKeyLogLabel = new Label ("ProcessColdKey log:") {
  141. X = Pos.Right (processHotKeyListView) + 1,
  142. Y = Pos.Top (editLabel) + 4,
  143. };
  144. Win.Add (processColdKeyLogLabel);
  145. yOffset = (Application.Top == Application.Top ? 1 : 6);
  146. var processColdKeyListView = new ListView (((TestWindow)Win)._processColdKeyList) {
  147. X = Pos.Left (processColdKeyLogLabel),
  148. Y = Pos.Top (processColdKeyLogLabel) + yOffset,
  149. Width = Dim.Percent (20),
  150. Height = Dim.Fill (),
  151. };
  152. Win.KeyDown += (a) => KeyDownPressUp (a.KeyEvent, "Down");
  153. Win.KeyPress += (a) => KeyDownPressUp (a.KeyEvent, "Press");
  154. Win.KeyUp += (a) => KeyDownPressUp (a.KeyEvent, "Up");
  155. void KeyDownPressUp (KeyEvent keyEvent, string updown)
  156. {
  157. var msg = $"Key{updown,-5}: {keyEvent}";
  158. keyStrokelist.Add (msg);
  159. keyStrokeListView.MoveDown ();
  160. processKeyListView.MoveDown ();
  161. processColdKeyListView.MoveDown ();
  162. processHotKeyListView.MoveDown ();
  163. }
  164. processColdKeyListView.ColorScheme = Colors.TopLevel;
  165. Win.Add (processColdKeyListView);
  166. }
  167. }
  168. }