SendKeys.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using System;
  2. using Terminal.Gui;
  3. namespace UICatalog.Scenarios;
  4. [ScenarioMetadata ("SendKeys", "SendKeys sample - Send key combinations.")]
  5. [ScenarioCategory ("Mouse and Keyboard")]
  6. public class SendKeys : Scenario
  7. {
  8. public override void Main ()
  9. {
  10. Application.Init ();
  11. var win = new Window { Title = GetQuitKeyAndName () };
  12. var label = new Label { X = Pos.Center (), Y = Pos.Center () - 6, Text = "Insert the text to send:" };
  13. win.Add (label);
  14. var txtInput = new TextField { X = Pos.Center (), Y = Pos.Center () - 5, Width = 20, Text = "MockKeyPresses" };
  15. win.Add (txtInput);
  16. var ckbShift = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 4, Text = "Shift" };
  17. win.Add (ckbShift);
  18. var ckbAlt = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 3, Text = "Alt" };
  19. win.Add (ckbAlt);
  20. var ckbControl = new CheckBox { X = Pos.Center (), Y = Pos.Center () - 2, Text = "Control" };
  21. win.Add (ckbControl);
  22. label = new Label { X = Pos.Center (), Y = Pos.Center () + 1, Text = "Result keys:" };
  23. win.Add (label);
  24. var txtResult = new TextField { X = Pos.Center (), Y = Pos.Center () + 2, Width = 20 };
  25. win.Add (txtResult);
  26. var rKeys = "";
  27. var rControlKeys = "";
  28. var IsShift = false;
  29. var IsAlt = false;
  30. var IsCtrl = false;
  31. txtResult.KeyDown += (s, e) =>
  32. {
  33. rKeys += (char)e.KeyCode;
  34. if (!IsShift && e.IsShift)
  35. {
  36. rControlKeys += " Shift ";
  37. IsShift = true;
  38. }
  39. if (!IsAlt && e.IsAlt)
  40. {
  41. rControlKeys += " Alt ";
  42. IsAlt = true;
  43. }
  44. if (!IsCtrl && e.IsCtrl)
  45. {
  46. rControlKeys += " Ctrl ";
  47. IsCtrl = true;
  48. }
  49. };
  50. var lblShippedKeys = new Label { X = Pos.Center (), Y = Pos.Center () + 3 };
  51. win.Add (lblShippedKeys);
  52. var lblShippedControlKeys = new Label { X = Pos.Center (), Y = Pos.Center () + 5 };
  53. win.Add (lblShippedControlKeys);
  54. var button = new Button { X = Pos.Center (), Y = Pos.Center () + 7, IsDefault = true, Text = "Process keys" };
  55. win.Add (button);
  56. void ProcessInput ()
  57. {
  58. rKeys = "";
  59. rControlKeys = "";
  60. txtResult.Text = "";
  61. IsShift = false;
  62. IsAlt = false;
  63. IsCtrl = false;
  64. txtResult.SetFocus ();
  65. foreach (char r in txtInput.Text)
  66. {
  67. ConsoleKey ck = char.IsLetter (r)
  68. ? (ConsoleKey)char.ToUpper (r)
  69. : (ConsoleKey)r;
  70. Application.Driver?.SendKeys (
  71. r,
  72. ck,
  73. ckbShift.State == CheckState.Checked,
  74. ckbAlt.State == CheckState.Checked,
  75. ckbControl.State == CheckState.Checked
  76. );
  77. }
  78. lblShippedKeys.Text = rKeys;
  79. lblShippedControlKeys.Text = rControlKeys;
  80. txtInput.SetFocus ();
  81. }
  82. button.Accept += (s, e) => ProcessInput ();
  83. win.KeyDown += (s, e) =>
  84. {
  85. if (e.KeyCode == KeyCode.Enter)
  86. {
  87. ProcessInput ();
  88. e.Handled = true;
  89. }
  90. };
  91. Application.Run (win);
  92. win.Dispose ();
  93. Application.Shutdown ();
  94. }
  95. }