Keys.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. using System.Collections.ObjectModel;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("Keys", "Shows keyboard input handling.")]
  5. [ScenarioCategory ("Mouse and Keyboard")]
  6. public class Keys : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. ObservableCollection<string> keyDownList = [];
  12. ObservableCollection<string> keyDownNotHandledList = 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.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  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. onKeyDownListView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  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. onKeyDownNotHandledListView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  119. win.Add (onKeyDownNotHandledListView);
  120. Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
  121. Application.KeyUp += (s, a) => KeyDownPressUp (a, "Up");
  122. void KeyDownPressUp (Key args, string updown)
  123. {
  124. var msg = $"Key{updown,-7}: {args}";
  125. keyList.Add (msg);
  126. appKeyListView.MoveDown ();
  127. onKeyDownNotHandledListView.MoveDown ();
  128. }
  129. Application.Run (win);
  130. win.Dispose ();
  131. Application.Shutdown ();
  132. }
  133. }