Class EscapeSequenceCharacterPattern

  • All Implemented Interfaces:
    CharacterPattern
    Direct Known Subclasses:
    ScreenInfoCharacterPattern

    public class EscapeSequenceCharacterPattern
    extends java.lang.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 Summary

      Fields 
      Modifier and Type Field Description
      static int ALT  
      static int CTRL  
      protected java.util.Map<java.lang.Character,​KeyType> finMap
      Map of recognized "finish pattern" sequences:
      e.g.: 'A' -> ArrowUp : "Esc [ A"
      static int SHIFT  
      protected java.util.Map<java.lang.Integer,​KeyType> stdMap
      Map of recognized "standard pattern" sequences:
      e.g.: 24 -> F12 : "Esc [ 24 ~"
      protected boolean useEscEsc
      A flag to control, whether an Esc-prefix for an Esc-sequence is to be treated as Alt-pressed.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected KeyStroke getKeyStroke​(KeyType key, int mods)
      combines a KeyType and modifiers into a KeyStroke.
      protected KeyStroke getKeyStrokeRaw​(char first, int num1, int num2, char last, boolean bEsc)
      combines the raw parts of the sequence into a KeyStroke.
      CharacterPattern.Matching match​(java.util.List<java.lang.Character> cur)
      Given a list of characters, determine whether it exactly matches any known KeyStroke, and whether a longer sequence can possibly match.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • stdMap

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

        protected final java.util.Map<java.lang.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 Detail

      • EscapeSequenceCharacterPattern

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

      • 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

        public CharacterPattern.Matching match​(java.util.List<java.lang.Character> cur)
        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