SendKeys.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System;
  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 += (char)e.KeyCode;
  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. ConsoleKey ck = char.IsLetter (r)
  67. ? (ConsoleKey)char.ToUpper (r)
  68. : (ConsoleKey)r;
  69. Application.Driver?.SendKeys (
  70. r,
  71. ck,
  72. ckbShift.CheckedState == CheckState.Checked,
  73. ckbAlt.CheckedState == CheckState.Checked,
  74. ckbControl.CheckedState == CheckState.Checked
  75. );
  76. }
  77. lblShippedKeys.Text = rKeys;
  78. lblShippedControlKeys.Text = rControlKeys;
  79. txtInput.SetFocus ();
  80. }
  81. button.Accepting += (s, e) => ProcessInput ();
  82. win.KeyDown += (s, e) =>
  83. {
  84. if (e.KeyCode == KeyCode.Enter)
  85. {
  86. ProcessInput ();
  87. e.Handled = true;
  88. }
  89. };
  90. Application.Run (win);
  91. win.Dispose ();
  92. Application.Shutdown ();
  93. }
  94. }