Keys.cs 5.3 KB

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