Keys.cs 5.5 KB

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