Keys.cs 5.6 KB

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