SendKeys.cs 4.3 KB

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