VkeyPacketSimulator.cs 14 KB

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