NetInputProcessor.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System.Collections.Concurrent;
  2. namespace Terminal.Gui.Drivers;
  3. /// <summary>
  4. /// Input processor for <see cref="NetInput"/>, deals in <see cref="ConsoleKeyInfo"/> stream
  5. /// </summary>
  6. public class NetInputProcessor : InputProcessor<ConsoleKeyInfo>
  7. {
  8. #pragma warning disable CA2211
  9. /// <summary>
  10. /// Set to true to generate code in <see cref="Logging"/> (verbose only) for test cases in NetInputProcessorTests.
  11. /// <remarks>
  12. /// This makes the task of capturing user/language/terminal specific keyboard issues easier to
  13. /// diagnose. By turning this on and searching logs user can send us exactly the input codes that are released
  14. /// to input stream.
  15. /// </remarks>
  16. /// </summary>
  17. public static bool GenerateTestCasesForKeyPresses = false;
  18. #pragma warning restore CA2211
  19. /// <inheritdoc/>
  20. public NetInputProcessor (ConcurrentQueue<ConsoleKeyInfo> inputBuffer) : base (inputBuffer, new NetKeyConverter ())
  21. {
  22. DriverName = "dotnet";
  23. }
  24. /// <inheritdoc/>
  25. protected override void Process (ConsoleKeyInfo consoleKeyInfo)
  26. {
  27. // For building test cases
  28. if (GenerateTestCasesForKeyPresses)
  29. {
  30. Logging.Trace (FormatConsoleKeyInfoForTestCase (consoleKeyInfo));
  31. }
  32. foreach (Tuple<char, ConsoleKeyInfo> released in Parser.ProcessInput (Tuple.Create (consoleKeyInfo.KeyChar, consoleKeyInfo)))
  33. {
  34. ProcessAfterParsing (released.Item2);
  35. }
  36. }
  37. /// <inheritdoc/>
  38. protected override void ProcessAfterParsing (ConsoleKeyInfo input)
  39. {
  40. var key = KeyConverter.ToKey (input);
  41. // If the key is not valid, we don't want to raise any events.
  42. if (IsValidInput (key, out key))
  43. {
  44. OnKeyDown (key);
  45. OnKeyUp (key);
  46. }
  47. }
  48. /* For building test cases */
  49. private static string FormatConsoleKeyInfoForTestCase (ConsoleKeyInfo input)
  50. {
  51. string charLiteral = input.KeyChar == '\0' ? @"'\0'" : $"'{input.KeyChar}'";
  52. return $"new ConsoleKeyInfo({charLiteral}, ConsoleKey.{input.Key}, "
  53. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Shift).ToString ().ToLower ()}, "
  54. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Alt).ToString ().ToLower ()}, "
  55. + $"{input.Modifiers.HasFlag (ConsoleModifiers.Control).ToString ().ToLower ()}),";
  56. }
  57. }