Keys.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. static List<string> _processKeyList = new List<string> ();
  9. static List<string> _processHotKeyList = new List<string> ();
  10. static List<string> _processColdKeyList = new List<string> ();
  11. class TestWindow : Window {
  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.ProcessHotKey (keyEvent);
  38. }
  39. }
  40. public override void Init (Toplevel top)
  41. {
  42. Top = top;
  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. };
  49. Top.Add (Win);
  50. }
  51. public override void Setup ()
  52. {
  53. // Type text here: ______
  54. var editLabel = new Label ("Type text here:") {
  55. X = 0,
  56. Y = 0,
  57. };
  58. Win.Add (editLabel);
  59. var edit = new TextField ("") {
  60. X = Pos.Right (editLabel) + 1,
  61. Y = Pos.Top (editLabel),
  62. Width = Dim.Fill (2),
  63. };
  64. Win.Add (edit);
  65. // Last KeyPress: ______
  66. var keyPressedLabel = new Label ("Last KeyPress:") {
  67. X = Pos.Left (editLabel),
  68. Y = Pos.Top (editLabel) + 2,
  69. };
  70. Win.Add (keyPressedLabel);
  71. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  72. var labelKeypress = new TextField ("") {
  73. X = Pos.Right (keyPressedLabel) + 1,
  74. Y = Pos.Top (keyPressedLabel),
  75. Width = 20,
  76. //TextAlignment = Terminal.Gui.TextAlignment.Left,
  77. ColorScheme = Colors.Error,
  78. };
  79. Win.Add (labelKeypress);
  80. Win.OnKeyPress += (KeyEvent keyEvent) => labelKeypress.Text = 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 yOffset = (Top == Application.Top ? 1 : 6);
  88. var keyStrokelist = new List<string> ();
  89. var keyStrokeListView = new ListView (keyStrokelist) {
  90. X = 0,
  91. Y = Pos.Top (keyLogLabel) + yOffset,
  92. Width = 25,
  93. Height = Dim.Fill (),
  94. };
  95. keyStrokeListView.ColorScheme = Colors.TopLevel;
  96. Win.Add (keyStrokeListView);
  97. void KeyDownPressUp (KeyEvent keyEvent, string updown)
  98. {
  99. var msg = $"Key{updown,-5}: {keyEvent.ToString ()}";
  100. keyStrokelist.Add (msg);
  101. keyStrokeListView.MoveDown ();
  102. }
  103. Win.OnKeyDown += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Down");
  104. Win.OnKeyPress += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Press");
  105. Win.OnKeyUp += (KeyEvent keyEvent) => KeyDownPressUp (keyEvent, "Up");
  106. // ProcessKey log:
  107. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  108. var processKeyLogLabel = new Label ("ProcessKey log:") {
  109. X = Pos.Right (keyStrokeListView) + 1,
  110. Y = Pos.Top (editLabel) + 4,
  111. };
  112. Win.Add (processKeyLogLabel);
  113. yOffset = (Top == Application.Top ? 1 : 6);
  114. var processKeyListView = new ListView (_processKeyList) {
  115. X = Pos.Left (processKeyLogLabel),
  116. Y = Pos.Top (processKeyLogLabel) + yOffset,
  117. Width = 25,
  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 = (Top == Application.Top ? 1 : 6);
  130. var processHotKeyListView = new ListView (_processHotKeyList) {
  131. X = Pos.Left (processHotKeyLogLabel),
  132. Y = Pos.Top (processHotKeyLogLabel) + yOffset,
  133. Width = 25,
  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 = (Top == Application.Top ? 1 : 6);
  146. var processColdKeyListView = new ListView (_processColdKeyList) {
  147. X = Pos.Left (processColdKeyLogLabel),
  148. Y = Pos.Top (processColdKeyLogLabel) + yOffset,
  149. Width = 25,
  150. Height = Dim.Fill (),
  151. };
  152. processColdKeyListView.ColorScheme = Colors.TopLevel;
  153. Win.Add (processColdKeyListView);
  154. }
  155. }
  156. }