2
0

AnsiKeyboardParser.cs 934 B

123456789101112131415161718192021222324252627
  1. #nullable enable
  2. namespace Terminal.Gui;
  3. /// <summary>
  4. /// Parses ANSI escape sequence strings that describe keyboard activity into <see cref="Key"/>.
  5. /// </summary>
  6. public class AnsiKeyboardParser
  7. {
  8. private readonly List<AnsiKeyboardParserPattern> _patterns = new ()
  9. {
  10. new Ss3Pattern (),
  11. new CsiKeyPattern (),
  12. new EscAsAltPattern { IsLastMinute = true }
  13. };
  14. /// <summary>
  15. /// Looks for any pattern that matches the <paramref name="input"/> and returns
  16. /// the matching pattern or <see langword="null"/> if no matches.
  17. /// </summary>
  18. /// <param name="input"></param>
  19. /// <param name="isLastMinute"></param>
  20. /// <returns></returns>
  21. public AnsiKeyboardParserPattern? IsKeyboard (string input, bool isLastMinute = false)
  22. {
  23. return _patterns.FirstOrDefault (pattern => pattern.IsLastMinute == isLastMinute && pattern.IsMatch (input));
  24. }
  25. }