Keys.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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.ProcessColdKey (keyEvent);
  38. }
  39. }
  40. public override void Init (Toplevel top)
  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. };
  50. 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. Width = 20,
  76. TextAlignment = Terminal.Gui.TextAlignment.Centered,
  77. ColorScheme = Colors.Error,
  78. };
  79. Win.Add (labelKeypress);
  80. Win.KeyPress += (sender, a) => labelKeypress.Text = a.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 = Dim.Percent (40),
  93. Height = Dim.Fill (),
  94. };
  95. keyStrokeListView.ColorScheme = Colors.TopLevel;
  96. Win.Add (keyStrokeListView);
  97. // ProcessKey log:
  98. var processKeyLogLabel = new Label ("ProcessKey log:") {
  99. X = Pos.Right (keyStrokeListView) + 1,
  100. Y = Pos.Top (editLabel) + 4,
  101. };
  102. Win.Add (processKeyLogLabel);
  103. yOffset = (Top == Application.Top ? 1 : 6);
  104. var processKeyListView = new ListView (_processKeyList) {
  105. X = Pos.Left (processKeyLogLabel),
  106. Y = Pos.Top (processKeyLogLabel) + yOffset,
  107. Width = Dim.Percent (60),
  108. Height = Dim.Fill (),
  109. };
  110. processKeyListView.ColorScheme = Colors.TopLevel;
  111. Win.Add (processKeyListView);
  112. // ProcessHotKey log:
  113. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  114. var processHotKeyLogLabel = new Label ("ProcessHotKey log:") {
  115. X = Pos.Right (processKeyListView) + 1,
  116. Y = Pos.Top (editLabel) + 4,
  117. };
  118. Win.Add (processHotKeyLogLabel);
  119. yOffset = (Top == Application.Top ? 1 : 6);
  120. var processHotKeyListView = new ListView (_processHotKeyList) {
  121. X = Pos.Left (processHotKeyLogLabel),
  122. Y = Pos.Top (processHotKeyLogLabel) + yOffset,
  123. Width = Dim.Percent (50),
  124. Height = Dim.Fill (),
  125. };
  126. processHotKeyListView.ColorScheme = Colors.TopLevel;
  127. Win.Add (processHotKeyListView);
  128. // ProcessColdKey log:
  129. // BUGBUG: Label is not positioning right with Pos, so using TextField instead
  130. var processColdKeyLogLabel = new Label ("ProcessColdKey log:") {
  131. X = Pos.Right (processHotKeyListView) + 1,
  132. Y = Pos.Top (editLabel) + 4,
  133. };
  134. Win.Add (processColdKeyLogLabel);
  135. yOffset = (Top == Application.Top ? 1 : 6);
  136. var processColdKeyListView = new ListView (_processColdKeyList) {
  137. X = Pos.Left (processColdKeyLogLabel),
  138. Y = Pos.Top (processColdKeyLogLabel) + yOffset,
  139. Width = Dim.Fill (),
  140. Height = Dim.Fill (),
  141. };
  142. Win.KeyDown += (sender, a) => KeyDownPressUp (a.KeyEvent, "Down");
  143. Win.KeyPress += (sender, a) => KeyDownPressUp (a.KeyEvent, "Press");
  144. Win.KeyUp += (sender, a) => KeyDownPressUp (a.KeyEvent, "Up");
  145. void KeyDownPressUp (KeyEvent keyEvent, string updown)
  146. {
  147. var msg = $"Key{updown,-5}: {keyEvent}";
  148. keyStrokelist.Add (msg);
  149. keyStrokeListView.MoveDown ();
  150. processKeyListView.MoveDown ();
  151. processColdKeyListView.MoveDown ();
  152. processHotKeyListView.MoveDown ();
  153. }
  154. processColdKeyListView.ColorScheme = Colors.TopLevel;
  155. Win.Add (processColdKeyListView);
  156. }
  157. }
  158. }