Keys.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. using System.Collections.ObjectModel;
  2. namespace UICatalog.Scenarios;
  3. [ScenarioMetadata ("Keys", "Shows keyboard input handling.")]
  4. [ScenarioCategory ("Mouse and Keyboard")]
  5. public class Keys : Scenario
  6. {
  7. public override void Main ()
  8. {
  9. Application.Init ();
  10. ObservableCollection<string> keyDownList = [];
  11. ObservableCollection<string> keyDownNotHandledList = new ();
  12. ObservableCollection<string> swallowedList = new ();
  13. var win = new Window { Title = GetQuitKeyAndName () };
  14. var label = new Label
  15. {
  16. X = 0,
  17. Y = 0,
  18. Text = "_Type text here:"
  19. };
  20. win.Add (label);
  21. var edit = new TextField
  22. {
  23. X = Pos.Right (label) + 1,
  24. Y = Pos.Top (label),
  25. Width = Dim.Fill (2),
  26. Height = 1,
  27. };
  28. win.Add (edit);
  29. label = new Label
  30. {
  31. X = 0,
  32. Y = Pos.Bottom (label),
  33. Text = "Last _Application.KeyDown:"
  34. };
  35. win.Add (label);
  36. var labelAppKeypress = new Label
  37. {
  38. X = Pos.Right (label) + 1,
  39. Y = Pos.Top (label)
  40. };
  41. win.Add (labelAppKeypress);
  42. Application.KeyDown += (s, e) => labelAppKeypress.Text = e.ToString ();
  43. label = new ()
  44. {
  45. X = 0,
  46. Y = Pos.Bottom (label),
  47. Text = "_Last TextField.KeyDown:"
  48. };
  49. win.Add (label);
  50. var lastTextFieldKeyDownLabel = new Label
  51. {
  52. X = Pos.Right (label) + 1,
  53. Y = Pos.Top (label),
  54. Height = 1,
  55. };
  56. win.Add (lastTextFieldKeyDownLabel);
  57. edit.KeyDown += (s, e) => lastTextFieldKeyDownLabel.Text = e.ToString ();
  58. // Application key event log:
  59. label = new Label
  60. {
  61. X = 0,
  62. Y = Pos.Bottom (label) + 1,
  63. Text = "Application Key Events:"
  64. };
  65. win.Add (label);
  66. int maxKeyString = Key.CursorRight.WithAlt.WithCtrl.WithShift.ToString ().Length;
  67. ObservableCollection<string> keyList = new ();
  68. var appKeyListView = new ListView
  69. {
  70. X = 0,
  71. Y = Pos.Bottom (label),
  72. Width = "KeyDown:".Length + maxKeyString,
  73. Height = Dim.Fill (),
  74. Source = new ListWrapper<string> (keyList)
  75. };
  76. appKeyListView.SchemeName = "Runnable";
  77. win.Add (appKeyListView);
  78. // View key events...
  79. edit.KeyDown += (s, a) => { keyDownList.Add (a.ToString ()); };
  80. edit.KeyDownNotHandled += (s, a) =>
  81. {
  82. keyDownNotHandledList.Add ($"{a}");
  83. };
  84. // KeyDown
  85. label = new Label
  86. {
  87. X = Pos.Right (appKeyListView) + 1,
  88. Y = Pos.Top (label),
  89. Text = "TextView Key Down:"
  90. };
  91. win.Add (label);
  92. var onKeyDownListView = new ListView
  93. {
  94. X = Pos.Left (label),
  95. Y = Pos.Bottom (label),
  96. Width = maxKeyString,
  97. Height = Dim.Fill (),
  98. Source = new ListWrapper<string> (keyDownList)
  99. };
  100. appKeyListView.SchemeName = "Runnable";
  101. win.Add (onKeyDownListView);
  102. // KeyDownNotHandled
  103. label = new Label
  104. {
  105. X = Pos.Right (onKeyDownListView) + 1,
  106. Y = Pos.Top (label),
  107. Text = "TextView KeyDownNotHandled:"
  108. };
  109. win.Add (label);
  110. var onKeyDownNotHandledListView = new ListView
  111. {
  112. X = Pos.Left (label),
  113. Y = Pos.Bottom (label),
  114. Width = maxKeyString,
  115. Height = Dim.Fill (),
  116. Source = new ListWrapper<string> (keyDownNotHandledList)
  117. };
  118. appKeyListView.SchemeName = "Runnable";
  119. win.Add (onKeyDownNotHandledListView);
  120. // Swallowed
  121. label = new Label
  122. {
  123. X = Pos.Right (onKeyDownNotHandledListView) + 1,
  124. Y = Pos.Top (label),
  125. Text = "Swallowed:"
  126. };
  127. win.Add (label);
  128. var onSwallowedListView = new ListView
  129. {
  130. X = Pos.Left (label),
  131. Y = Pos.Bottom (label),
  132. Width = maxKeyString,
  133. Height = Dim.Fill (),
  134. Source = new ListWrapper<string> (swallowedList)
  135. };
  136. appKeyListView.SchemeName = "Runnable";
  137. win.Add (onSwallowedListView);
  138. Application.Driver!.InputProcessor.AnsiSequenceSwallowed += (s, e) => { swallowedList.Add (e.Replace ("\x1b", "Esc")); };
  139. Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
  140. Application.KeyUp += (s, a) => KeyDownPressUp (a, "Up");
  141. void KeyDownPressUp (Key args, string updown)
  142. {
  143. var msg = $"Key{updown,-7}: {args}";
  144. keyList.Add (msg);
  145. appKeyListView.MoveDown ();
  146. onKeyDownNotHandledListView.MoveDown ();
  147. }
  148. Application.Run (win);
  149. win.Dispose ();
  150. Application.Shutdown ();
  151. }
  152. }