VkeyPacketSimulator.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;
  4. using System.Threading.Tasks;
  5. using Terminal.Gui;
  6. using Terminal.Gui.ConsoleDrivers;
  7. namespace UICatalog.Scenarios;
  8. [ScenarioMetadata ("VkeyPacketSimulator", "Simulates the Virtual Key Packet")]
  9. [ScenarioCategory ("Mouse and Keyboard")]
  10. public class VkeyPacketSimulator : Scenario
  11. {
  12. private static readonly ManualResetEventSlim _stopOutput = new (false);
  13. private readonly List<KeyCode> _keyboardStrokes = new ();
  14. private bool _outputStarted;
  15. private bool _wasUnknown;
  16. public override void Setup ()
  17. {
  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), AutoSize = false, Width = Dim.Fill (), ColorScheme = Colors.ColorSchemes ["Error"]
  26. };
  27. Win.Add (inputHorizontalRuler);
  28. var inputVerticalRuler = new Label
  29. {
  30. Y = Pos.Bottom (btnInput),
  31. AutoSize = false,
  32. Width = 1,
  33. ColorScheme = Colors.ColorSchemes ["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 Label { 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. AutoSize = false,
  54. Width = Dim.Fill (),
  55. ColorScheme = Colors.ColorSchemes ["Error"]
  56. };
  57. Win.Add (outputHorizontalRuler);
  58. var outputVerticalRuler = new Label
  59. {
  60. Y = Pos.Bottom (btnOutput),
  61. AutoSize = false,
  62. Width = 1,
  63. Height = Dim.Fill (),
  64. ColorScheme = Colors.ColorSchemes ["Error"],
  65. TextDirection = TextDirection.TopBottom_LeftRight
  66. };
  67. Win.Add (outputVerticalRuler);
  68. var tvOutput = new TextView
  69. {
  70. Title = "Output",
  71. X = 1,
  72. Y = Pos.Bottom (outputHorizontalRuler),
  73. Width = Dim.Fill (),
  74. Height = Dim.Fill (),
  75. ReadOnly = true
  76. };
  77. // Detect unknown keys and reject them.
  78. tvOutput.KeyDown += (s, e) =>
  79. {
  80. //System.Diagnostics.Debug.WriteLine ($"Output - KeyDown: {e.KeyCode}");
  81. if (e.NoAlt.NoCtrl.NoShift == KeyCode.Null)
  82. {
  83. _wasUnknown = true;
  84. e.Handled = true;
  85. return;
  86. }
  87. //System.Diagnostics.Debug.WriteLine ($"Output - KeyPress - _keyboardStrokes: {_keyboardStrokes.Count}");
  88. if (_outputStarted)
  89. {
  90. // If the key wasn't handled by the TextView will popup a Dialog with the keys pressed.
  91. bool? handled = tvOutput.OnInvokingKeyBindings (e);
  92. if (handled == null || handled == false)
  93. {
  94. if (!tvOutput.OnProcessKeyDown (e))
  95. {
  96. Application.Invoke (
  97. () => MessageBox.Query (
  98. "Keys",
  99. $"'{
  100. Key.ToString (
  101. e.KeyCode,
  102. MenuBar.ShortcutDelimiter
  103. )
  104. }' pressed!",
  105. "Ok"
  106. )
  107. );
  108. }
  109. }
  110. }
  111. e.Handled = true;
  112. _stopOutput.Set ();
  113. };
  114. Win.Add (tvOutput);
  115. tvInput.KeyDown += (s, e) =>
  116. {
  117. //System.Diagnostics.Debug.WriteLine ($"Input - KeyDown: {e.KeyCode.Key}");
  118. if (e.KeyCode == Key.Empty)
  119. {
  120. _wasUnknown = true;
  121. e.Handled = true;
  122. }
  123. else
  124. {
  125. _wasUnknown = false;
  126. }
  127. };
  128. tvInput.InvokingKeyBindings += (s, e) =>
  129. {
  130. Key ev = e;
  131. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress: {ev}");
  132. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress - _keyboardStrokes: {_keyboardStrokes.Count}");
  133. if (!e.IsValid)
  134. {
  135. _wasUnknown = true;
  136. e.Handled = true;
  137. return;
  138. }
  139. _keyboardStrokes.Add (e.KeyCode);
  140. };
  141. tvInput.KeyUp += (s, e) =>
  142. {
  143. //System.Diagnostics.Debug.WriteLine ($"Input - KeyUp: {e.Key}");
  144. e.Handled = true;
  145. if (!_wasUnknown && _keyboardStrokes.Count > 0)
  146. {
  147. _outputStarted = true;
  148. tvOutput.ReadOnly = false;
  149. tvOutput.SetFocus ();
  150. tvOutput.SetNeedsDisplay ();
  151. Task.Run (
  152. () =>
  153. {
  154. while (_outputStarted)
  155. {
  156. try
  157. {
  158. while (_keyboardStrokes.Count > 0)
  159. {
  160. if (_keyboardStrokes [0] == KeyCode.Null)
  161. {
  162. continue;
  163. }
  164. ConsoleKeyInfo consoleKeyInfo =
  165. ConsoleKeyMapping.GetConsoleKeyInfoFromKeyCode (_keyboardStrokes [0]);
  166. char keyChar =
  167. ConsoleKeyMapping.EncodeKeyCharForVKPacket (consoleKeyInfo);
  168. Application.Driver.SendKeys (
  169. keyChar,
  170. ConsoleKey.Packet,
  171. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift),
  172. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
  173. consoleKeyInfo.Modifiers
  174. .HasFlag (ConsoleModifiers.Control)
  175. );
  176. _stopOutput.Wait ();
  177. _stopOutput.Reset ();
  178. _keyboardStrokes.RemoveAt (0);
  179. Application.Invoke (
  180. () =>
  181. {
  182. tvOutput.ReadOnly = true;
  183. tvInput.SetFocus ();
  184. }
  185. );
  186. }
  187. _outputStarted = false;
  188. }
  189. catch (Exception)
  190. {
  191. Application.Invoke (
  192. () =>
  193. {
  194. MessageBox.ErrorQuery (
  195. "Error",
  196. "Couldn't send the keystrokes!",
  197. "Ok"
  198. );
  199. Application.RequestStop ();
  200. }
  201. );
  202. }
  203. }
  204. //System.Diagnostics.Debug.WriteLine ($"_outputStarted: {_outputStarted}");
  205. }
  206. );
  207. }
  208. };
  209. btnInput.Accept += (s, e) =>
  210. {
  211. if (!tvInput.HasFocus && _keyboardStrokes.Count == 0)
  212. {
  213. tvInput.SetFocus ();
  214. }
  215. };
  216. btnOutput.Accept += (s, e) =>
  217. {
  218. if (!tvOutput.HasFocus && _keyboardStrokes.Count == 0)
  219. {
  220. tvOutput.SetFocus ();
  221. }
  222. };
  223. tvInput.SetFocus ();
  224. void Win_LayoutComplete (object sender, LayoutEventArgs obj)
  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.LayoutComplete += Win_LayoutComplete;
  243. }
  244. }