Class EscapeSequenceCharacterPattern

java.lang.Object
com.googlecode.lanterna.input.EscapeSequenceCharacterPattern
All Implemented Interfaces:
CharacterPattern
Direct Known Subclasses:
ScreenInfoCharacterPattern

public class EscapeSequenceCharacterPattern extends Object implements CharacterPattern
This implementation of CharacterPattern matches two similar patterns of Escape sequences, that many terminals produce for special keys.

These sequences all start with Escape, followed by either an open bracket or a capital letter O (these two are treated as equivalent).

Then follows a list of zero or up to two decimals separated by a semicolon, and a non-digit last character.

If the last character is a tilde (~) then the first number defines the key (through stdMap), otherwise the last character itself defines the key (through finMap).

The second number, if provided by the terminal, specifies the modifier state (shift,alt,ctrl). The value is 1 + sum(modifiers), where shift is 1, alt is 2 and ctrl is 4.

The two maps stdMap and finMap can be customized in subclasses to add, remove or replace keys - to support non-standard Terminals.

Examples: (on a gnome terminal)
ArrowUp is "Esc [ A"; Alt-ArrowUp is "Esc [ 1 ; 3 A"
both are handled by finMap mapping 'A' to ArrowUp

F6 is "Esc [ 1 7 ~"; Ctrl-Shift-F6 is "Esc [ 1 7 ; 6 R"
both are handled by stdMap mapping 17 to F6

  • Field Details

    • SHIFT

      public static final int SHIFT
      See Also:
    • ALT

      public static final int ALT
      See Also:
    • CTRL

      public static final int CTRL
      See Also:
    • stdMap

      protected final Map<Integer,KeyType> stdMap
      Map of recognized "standard pattern" sequences:
      e.g.: 24 -> F12 : "Esc [ 24 ~"
    • finMap

      protected final Map<Character,KeyType> finMap
      Map of recognized "finish pattern" sequences:
      e.g.: 'A' -> ArrowUp : "Esc [ A"
    • useEscEsc

      protected boolean useEscEsc
      A flag to control, whether an Esc-prefix for an Esc-sequence is to be treated as Alt-pressed. Some Terminals (e.g. putty) report the Alt-modifier like that.

      If the application is e.g. more interested in seeing separate Escape and plain Arrow keys, then it should replace this class by a subclass that sets this flag to false. (It might then also want to remove the CtrlAltAndCharacterPattern.)

  • Constructor Details

    • EscapeSequenceCharacterPattern

      public EscapeSequenceCharacterPattern()
      Create an instance with a standard set of mappings.
  • Method Details

    • getKeyStroke

      protected KeyStroke getKeyStroke(KeyType key, int mods)
      combines a KeyType and modifiers into a KeyStroke. Subclasses can override this for customization purposes.
      Parameters:
      key - the KeyType as determined by parsing the sequence. It will be null, if the pattern looked like a key sequence but wasn't identified.
      mods - the bitmask of the modifer keys pressed along with the key.
      Returns:
      either null (to report mis-match), or a valid KeyStroke.
    • getKeyStrokeRaw

      protected KeyStroke getKeyStrokeRaw(char first, int num1, int num2, char last, boolean bEsc)
      combines the raw parts of the sequence into a KeyStroke. This method does not check the first char, but overrides may do so.
      Parameters:
      first - the char following after Esc in the sequence (either [ or O)
      num1 - the first decimal, or 0 if not in the sequence
      num2 - the second decimal, or 0 if not in the sequence
      last - the terminating char.
      bEsc - whether an extra Escape-prefix was found.
      Returns:
      either null (to report mis-match), or a valid KeyStroke.
    • match

      Description copied from interface: CharacterPattern
      Given a list of characters, determine whether it exactly matches any known KeyStroke, and whether a longer sequence can possibly match.
      Specified by:
      match in interface CharacterPattern
      Parameters:
      cur - of characters to check
      Returns:
      see Matching