VkeyPacketSimulator.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 Main ()
  17. {
  18. Application.Init ();
  19. var win = new Window { Title = GetQuitKeyAndName () };
  20. var label = new Label { X = Pos.Center (), Text = "Input" };
  21. win.Add (label);
  22. var btnInput = new Button { X = Pos.AnchorEnd (16), Text = "Select Input" };
  23. win.Add (btnInput);
  24. const string ruler = "|123456789";
  25. var inputHorizontalRuler = new Label
  26. {
  27. Y = Pos.Bottom (btnInput), Width = Dim.Fill (), ColorScheme = Colors.ColorSchemes ["Error"]
  28. };
  29. win.Add (inputHorizontalRuler);
  30. var inputVerticalRuler = new Label
  31. {
  32. Y = Pos.Bottom (btnInput),
  33. Width = 1,
  34. Height = Dim.Percent (50),
  35. ColorScheme = Colors.ColorSchemes ["Error"],
  36. TextDirection = TextDirection.TopBottom_LeftRight
  37. };
  38. win.Add (inputVerticalRuler);
  39. var tvInput = new TextView
  40. {
  41. Title = "Input",
  42. X = 1,
  43. Y = Pos.Bottom (inputHorizontalRuler),
  44. Width = Dim.Fill (),
  45. Height = Dim.Percent (50) - 1
  46. };
  47. win.Add (tvInput);
  48. label = new() { X = Pos.Center (), Y = Pos.Bottom (tvInput), Text = "Output" };
  49. win.Add (label);
  50. var btnOutput = new Button { X = Pos.AnchorEnd (17), Y = Pos.Top (label), Text = "Select Output" };
  51. win.Add (btnOutput);
  52. var outputHorizontalRuler = new Label
  53. {
  54. Y = Pos.Bottom (btnOutput),
  55. Width = Dim.Fill (),
  56. ColorScheme = Colors.ColorSchemes ["Error"]
  57. };
  58. win.Add (outputHorizontalRuler);
  59. var outputVerticalRuler = new Label
  60. {
  61. Y = Pos.Bottom(btnOutput),
  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, KeyBindingScope.HotKey | KeyBindingScope.Focused);
  92. if (handled == null || handled == false)
  93. {
  94. if (!tvOutput.OnProcessKeyDown (e))
  95. {
  96. Application.Invoke (
  97. () => MessageBox.Query (
  98. "Keys",
  99. $"'{Key.ToString (
  100. e.KeyCode,
  101. MenuBar.ShortcutDelimiter
  102. )}' pressed!",
  103. "Ok"
  104. )
  105. );
  106. }
  107. }
  108. }
  109. e.Handled = true;
  110. _stopOutput.Set ();
  111. };
  112. win.Add (tvOutput);
  113. tvInput.KeyDown += (s, e) =>
  114. {
  115. //System.Diagnostics.Debug.WriteLine ($"Input - KeyDown: {e.KeyCode.Key}");
  116. if (e.KeyCode == Key.Empty)
  117. {
  118. _wasUnknown = true;
  119. e.Handled = true;
  120. }
  121. else
  122. {
  123. _wasUnknown = false;
  124. }
  125. };
  126. tvInput.InvokingKeyBindings += (s, e) =>
  127. {
  128. Key ev = e;
  129. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress: {ev}");
  130. //System.Diagnostics.Debug.WriteLine ($"Input - KeyPress - _keyboardStrokes: {_keyboardStrokes.Count}");
  131. if (!e.IsValid)
  132. {
  133. _wasUnknown = true;
  134. e.Handled = true;
  135. return;
  136. }
  137. _keyboardStrokes.Add (e.KeyCode);
  138. };
  139. tvInput.KeyUp += (s, e) =>
  140. {
  141. //System.Diagnostics.Debug.WriteLine ($"Input - KeyUp: {e.Key}");
  142. e.Handled = true;
  143. if (!_wasUnknown && _keyboardStrokes.Count > 0)
  144. {
  145. _outputStarted = true;
  146. tvOutput.ReadOnly = false;
  147. tvOutput.SetFocus ();
  148. tvOutput.SetNeedsDisplay ();
  149. Task.Run (
  150. () =>
  151. {
  152. while (_outputStarted)
  153. {
  154. try
  155. {
  156. while (_keyboardStrokes.Count > 0)
  157. {
  158. if (_keyboardStrokes [0] == KeyCode.Null)
  159. {
  160. continue;
  161. }
  162. ConsoleKeyInfo consoleKeyInfo =
  163. ConsoleKeyMapping.GetConsoleKeyInfoFromKeyCode (_keyboardStrokes [0]);
  164. char keyChar =
  165. ConsoleKeyMapping.EncodeKeyCharForVKPacket (consoleKeyInfo);
  166. Application.Driver.SendKeys (
  167. keyChar,
  168. ConsoleKey.Packet,
  169. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Shift),
  170. consoleKeyInfo.Modifiers.HasFlag (ConsoleModifiers.Alt),
  171. consoleKeyInfo.Modifiers
  172. .HasFlag (ConsoleModifiers.Control)
  173. );
  174. _stopOutput.Wait ();
  175. _stopOutput.Reset ();
  176. _keyboardStrokes.RemoveAt (0);
  177. Application.Invoke (
  178. () =>
  179. {
  180. tvOutput.ReadOnly = true;
  181. tvInput.SetFocus ();
  182. }
  183. );
  184. }
  185. _outputStarted = false;
  186. }
  187. catch (Exception)
  188. {
  189. Application.Invoke (
  190. () =>
  191. {
  192. MessageBox.ErrorQuery (
  193. "Error",
  194. "Couldn't send the keystrokes!",
  195. "Ok"
  196. );
  197. Application.RequestStop ();
  198. }
  199. );
  200. }
  201. }
  202. //System.Diagnostics.Debug.WriteLine ($"_outputStarted: {_outputStarted}");
  203. }
  204. );
  205. }
  206. };
  207. btnInput.Accept += (s, e) =>
  208. {
  209. if (!tvInput.HasFocus && _keyboardStrokes.Count == 0)
  210. {
  211. tvInput.SetFocus ();
  212. }
  213. };
  214. btnOutput.Accept += (s, e) =>
  215. {
  216. if (!tvOutput.HasFocus && _keyboardStrokes.Count == 0)
  217. {
  218. tvOutput.SetFocus ();
  219. }
  220. };
  221. tvInput.SetFocus ();
  222. void Win_LayoutComplete (object sender, LayoutEventArgs obj)
  223. {
  224. if (inputHorizontalRuler.Viewport.Width == 0 || inputVerticalRuler.Viewport.Height == 0)
  225. {
  226. return;
  227. }
  228. inputHorizontalRuler.Text = outputHorizontalRuler.Text =
  229. ruler.Repeat (
  230. (int)Math.Ceiling (
  231. inputHorizontalRuler.Viewport.Width
  232. / (double)ruler.Length
  233. )
  234. ) [
  235. ..inputHorizontalRuler.Viewport.Width];
  236. inputVerticalRuler.Height = tvInput.Frame.Height + 1;
  237. inputVerticalRuler.Text =
  238. ruler.Repeat ((int)Math.Ceiling (inputVerticalRuler.Viewport.Height / (double)ruler.Length)) [
  239. ..inputVerticalRuler.Viewport.Height];
  240. outputVerticalRuler.Text =
  241. ruler.Repeat ((int)Math.Ceiling (outputVerticalRuler.Viewport.Height / (double)ruler.Length)) [
  242. ..outputVerticalRuler.Viewport.Height];
  243. }
  244. win.LayoutComplete += Win_LayoutComplete;
  245. Application.Run (win);
  246. win.Dispose ();
  247. Application.Shutdown ();
  248. }
  249. }