VkeyPacketSimulator.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. namespace UICatalog.Scenarios;
  6. [ScenarioMetadata ("VkeyPacketSimulator", "Simulates the Virtual Key Packet")]
  7. [ScenarioCategory ("Mouse and Keyboard")]
  8. public class VkeyPacketSimulator : Scenario
  9. {
  10. private static readonly ManualResetEventSlim _stopOutput = new (false);
  11. private readonly List<KeyCode> _keyboardStrokes = new ();
  12. private bool _outputStarted;
  13. private bool _wasUnknown;
  14. public override void Main ()
  15. {
  16. Application.Init ();
  17. var win = new Window { Title = GetQuitKeyAndName () };
  18. var label = new Label { X = Pos.Center (), Text = "Input" };
  19. win.Add (label);
  20. var btnInput = new Button { X = Pos.AnchorEnd (16), Text = "Select Input" };
  21. win.Add (btnInput);
  22. const string ruler = "|123456789";
  23. var inputHorizontalRuler = new Label
  24. {
  25. Y = Pos.Bottom (btnInput), Width = Dim.Fill (), SchemeName = "Error"
  26. };
  27. win.Add (inputHorizontalRuler);
  28. var inputVerticalRuler = new Label
  29. {
  30. Y = Pos.Bottom (btnInput),
  31. Width = 1,
  32. Height = Dim.Percent (50),
  33. SchemeName = "Error",
  34. TextDirection = TextDirection.TopBottom_LeftRight
  35. };
  36. win.Add (inputVerticalRuler);
  37. var tvInput = new TextView
  38. {
  39. Title = "Input",
  40. X = 1,
  41. Y = Pos.Bottom (inputHorizontalRuler),
  42. Width = Dim.Fill (),
  43. Height = Dim.Percent (50) - 1
  44. };
  45. win.Add (tvInput);
  46. label = new() { X = Pos.Center (), Y = Pos.Bottom (tvInput), Text = "Output" };
  47. win.Add (label);
  48. var btnOutput = new Button { X = Pos.AnchorEnd (17), Y = Pos.Top (label), Text = "Select Output" };
  49. win.Add (btnOutput);
  50. var outputHorizontalRuler = new Label
  51. {
  52. Y = Pos.Bottom (btnOutput),
  53. Width = Dim.Fill (),
  54. SchemeName = "Error"
  55. };
  56. win.Add (outputHorizontalRuler);
  57. var outputVerticalRuler = new Label
  58. {
  59. Y = Pos.Bottom(btnOutput),
  60. Width = 1,
  61. Height = Dim.Fill (),
  62. SchemeName = "Error",
  63. TextDirection = TextDirection.TopBottom_LeftRight
  64. };
  65. win.Add (outputVerticalRuler);
  66. var tvOutput = new TextView
  67. {
  68. Title = "Output",
  69. X = 1,
  70. Y = Pos.Bottom (outputHorizontalRuler),
  71. Width = Dim.Fill (),
  72. Height = Dim.Fill (),
  73. ReadOnly = true
  74. };
  75. // Detect unknown keys and reject them.
  76. tvOutput.KeyDown += (s, e) =>
  77. {
  78. //System.Diagnostics.Debug.WriteLine ($"Output - KeyDown: {e.KeyCode}");
  79. if (e.NoAlt.NoCtrl.NoShift == KeyCode.Null)
  80. {
  81. _wasUnknown = true;
  82. e.Handled = true;
  83. return;
  84. }
  85. //System.Diagnostics.Debug.WriteLine ($"Output - KeyPress - _keyboardStrokes: {_keyboardStrokes.Count}");
  86. if (_outputStarted)
  87. {
  88. // If the key wasn't handled by the TextView will popup a Dialog with the keys pressed.
  89. bool? handled = tvOutput.NewKeyDownEvent (e);
  90. if (handled == null || handled == false)
  91. {
  92. if (!tvOutput.NewKeyDownEvent (e))
  93. {
  94. Application.Invoke (
  95. () => MessageBox.Query (
  96. "Keys",
  97. $"'{Key.ToString (
  98. e.KeyCode,
  99. Key.Separator
  100. )}' pressed!",
  101. "Ok"
  102. )
  103. );
  104. }
  105. }
  106. }
  107. e.Handled = true;
  108. _stopOutput.Set ();
  109. };
  110. win.Add (tvOutput);
  111. tvInput.KeyDown += (s, e) =>
  112. {
  113. //System.Diagnostics.Debug.WriteLine ($"Input - KeyDown: {e.KeyCode.Key}");
  114. if (e.KeyCode == Key.Empty)
  115. {
  116. _wasUnknown = true;
  117. e.Handled = true;
  118. }
  119. else
  120. {
  121. _wasUnknown = false;
  122. }
  123. };
  124. tvInput.KeyDownNotHandled += (s, e) =>
  125. {
  126. Key ev = e;
  127. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress: {ev}");
  128. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress - _keyboardStrokes: {_keyboardStrokes.Count}");
  129. if (!e.IsValid)
  130. {
  131. _wasUnknown = true;
  132. e.Handled = true;
  133. return;
  134. }
  135. _keyboardStrokes.Add (e.KeyCode);
  136. };
  137. tvInput.KeyUp += (s, e) =>
  138. {
  139. //System.Diagnostics.Debug.WriteLine ($"Input - KeyUp: {e.Key}");
  140. e.Handled = true;
  141. if (!_wasUnknown && _keyboardStrokes.Count > 0)
  142. {
  143. _outputStarted = true;
  144. tvOutput.ReadOnly = false;
  145. tvOutput.SetFocus ();
  146. tvOutput.SetNeedsDraw ();
  147. Task.Run (
  148. () =>
  149. {
  150. while (_outputStarted)
  151. {
  152. try
  153. {
  154. while (_keyboardStrokes.Count > 0)
  155. {
  156. if (_keyboardStrokes [0] == KeyCode.Null)
  157. {
  158. continue;
  159. }
  160. ConsoleKeyInfo consoleKeyInfo =
  161. ConsoleKeyMapping.GetConsoleKeyInfoFromKeyCode (_keyboardStrokes [0]);
  162. char keyChar =
  163. ConsoleKeyMapping.EncodeKeyCharForVKPacket (consoleKeyInfo);
  164. Application.Driver?.SendKeys (
  165. keyChar,
  166. ConsoleKey.Packet,
  167. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift),
  168. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
  169. consoleKeyInfo.Modifiers
  170. .HasFlag (ConsoleModifiers.Control)
  171. );
  172. _stopOutput.Wait ();
  173. _stopOutput.Reset ();
  174. _keyboardStrokes.RemoveAt (0);
  175. Application.Invoke (
  176. () =>
  177. {
  178. tvOutput.ReadOnly = true;
  179. tvInput.SetFocus ();
  180. }
  181. );
  182. }
  183. _outputStarted = false;
  184. }
  185. catch (Exception)
  186. {
  187. Application.Invoke (
  188. () =>
  189. {
  190. MessageBox.ErrorQuery (
  191. "Error",
  192. "Couldn't send the keystrokes!",
  193. "Ok"
  194. );
  195. Application.RequestStop ();
  196. }
  197. );
  198. }
  199. }
  200. //System.Diagnostics.Debug.WriteLine ($"_outputStarted: {_outputStarted}");
  201. }
  202. );
  203. }
  204. };
  205. btnInput.Accepting += (s, e) =>
  206. {
  207. if (!tvInput.HasFocus && _keyboardStrokes.Count == 0)
  208. {
  209. tvInput.SetFocus ();
  210. }
  211. };
  212. btnOutput.Accepting += (s, e) =>
  213. {
  214. if (!tvOutput.HasFocus && _keyboardStrokes.Count == 0)
  215. {
  216. tvOutput.SetFocus ();
  217. }
  218. };
  219. tvInput.SetFocus ();
  220. void Win_LayoutComplete (object sender, LayoutEventArgs obj)
  221. {
  222. if (inputHorizontalRuler.Viewport.Width == 0 || inputVerticalRuler.Viewport.Height == 0)
  223. {
  224. return;
  225. }
  226. inputHorizontalRuler.Text = outputHorizontalRuler.Text =
  227. ruler.Repeat (
  228. (int)Math.Ceiling (
  229. inputHorizontalRuler.Viewport.Width
  230. / (double)ruler.Length
  231. )
  232. ) [
  233. ..inputHorizontalRuler.Viewport.Width];
  234. inputVerticalRuler.Height = tvInput.Frame.Height + 1;
  235. inputVerticalRuler.Text =
  236. ruler.Repeat ((int)Math.Ceiling (inputVerticalRuler.Viewport.Height / (double)ruler.Length)) [
  237. ..inputVerticalRuler.Viewport.Height];
  238. outputVerticalRuler.Text =
  239. ruler.Repeat ((int)Math.Ceiling (outputVerticalRuler.Viewport.Height / (double)ruler.Length)) [
  240. ..outputVerticalRuler.Viewport.Height];
  241. }
  242. win.SubViewsLaidOut += Win_LayoutComplete;
  243. Application.Run (win);
  244. win.Dispose ();
  245. Application.Shutdown ();
  246. }
  247. }