Keys.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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> keyPressedList = [];
  12. ObservableCollection<string> invokingKeyBindingsList = new ();
  13. var win = new Window { Title = GetQuitKeyAndName () };
  14. var editLabel = new Label { X = 0, Y = 0, Text = "Type text here:" };
  15. win.Add (editLabel);
  16. var edit = new TextField { X = Pos.Right (editLabel) + 1, Y = Pos.Top (editLabel), Width = Dim.Fill (2) };
  17. win.Add (edit);
  18. edit.KeyDown += (s, a) => { keyPressedList.Add (a.ToString ()); };
  19. edit.InvokingKeyBindings += (s, a) =>
  20. {
  21. if (edit.KeyBindings.TryGet (a, out KeyBinding binding))
  22. {
  23. invokingKeyBindingsList.Add ($"{a}: {string.Join (",", binding.Commands)}");
  24. }
  25. };
  26. // Last KeyPress: ______
  27. var keyPressedLabel = new Label
  28. {
  29. X = Pos.Left (editLabel), Y = Pos.Top (editLabel) + 1, Text = "Last TextView.KeyPressed:"
  30. };
  31. win.Add (keyPressedLabel);
  32. var labelTextViewKeypress = new Label { X = Pos.Right (keyPressedLabel) + 1, Y = Pos.Top (keyPressedLabel) };
  33. win.Add (labelTextViewKeypress);
  34. edit.KeyDown += (s, e) => labelTextViewKeypress.Text = e.ToString ();
  35. keyPressedLabel = new Label
  36. {
  37. X = Pos.Left (keyPressedLabel), Y = Pos.Bottom (keyPressedLabel), Text = "Last Application.KeyDown:"
  38. };
  39. win.Add (keyPressedLabel);
  40. var labelAppKeypress = new Label { X = Pos.Right (keyPressedLabel) + 1, Y = Pos.Top (keyPressedLabel) };
  41. win.Add (labelAppKeypress);
  42. Application.KeyDown += (s, e) => labelAppKeypress.Text = e.ToString ();
  43. // Key stroke log:
  44. var keyLogLabel = new Label
  45. {
  46. X = Pos.Left (editLabel), Y = Pos.Top (editLabel) + 4, Text = "Application Key Events:"
  47. };
  48. win.Add (keyLogLabel);
  49. int maxKeyString = Key.CursorRight.WithAlt.WithCtrl.WithShift.ToString ().Length;
  50. var yOffset = 1;
  51. ObservableCollection<string> keyEventlist = new ();
  52. var keyEventListView = new ListView
  53. {
  54. X = 0,
  55. Y = Pos.Top (keyLogLabel) + yOffset,
  56. Width = "Key Down:".Length + maxKeyString,
  57. Height = Dim.Fill (),
  58. Source = new ListWrapper<string> (keyEventlist)
  59. };
  60. keyEventListView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  61. win.Add (keyEventListView);
  62. // OnKeyPressed
  63. var onKeyPressedLabel = new Label
  64. {
  65. X = Pos.Right (keyEventListView) + 1, Y = Pos.Top (editLabel) + 4, Text = "TextView KeyDown:"
  66. };
  67. win.Add (onKeyPressedLabel);
  68. yOffset = 1;
  69. var onKeyPressedListView = new ListView
  70. {
  71. X = Pos.Left (onKeyPressedLabel),
  72. Y = Pos.Top (onKeyPressedLabel) + yOffset,
  73. Width = maxKeyString,
  74. Height = Dim.Fill (),
  75. Source = new ListWrapper<string> (keyPressedList)
  76. };
  77. onKeyPressedListView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  78. win.Add (onKeyPressedListView);
  79. // OnInvokeKeyBindings
  80. var onInvokingKeyBindingsLabel = new Label
  81. {
  82. X = Pos.Right (onKeyPressedListView) + 1,
  83. Y = Pos.Top (editLabel) + 4,
  84. Text = "TextView InvokingKeyBindings:"
  85. };
  86. win.Add (onInvokingKeyBindingsLabel);
  87. var onInvokingKeyBindingsListView = new ListView
  88. {
  89. X = Pos.Left (onInvokingKeyBindingsLabel),
  90. Y = Pos.Top (onInvokingKeyBindingsLabel) + yOffset,
  91. Width = Dim.Fill (1),
  92. Height = Dim.Fill (),
  93. Source = new ListWrapper<string> (invokingKeyBindingsList)
  94. };
  95. onInvokingKeyBindingsListView.ColorScheme = Colors.ColorSchemes ["TopLevel"];
  96. win.Add (onInvokingKeyBindingsListView);
  97. //Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
  98. Application.KeyDown += (s, a) => KeyDownPressUp (a, "Down");
  99. Application.KeyUp += (s, a) => KeyDownPressUp (a, "Up");
  100. void KeyDownPressUp (Key args, string updown)
  101. {
  102. // BUGBUG: KeyEvent.ToString is badly broken
  103. var msg = $"Key{updown,-7}: {args}";
  104. keyEventlist.Add (msg);
  105. keyEventListView.MoveDown ();
  106. onKeyPressedListView.MoveDown ();
  107. onInvokingKeyBindingsListView.MoveDown ();
  108. }
  109. Application.Run (win);
  110. win.Dispose ();
  111. Application.Shutdown ();
  112. }
  113. }