Class KeyStroke

  • Direct Known Subclasses:
    KeyStroke.RealF3, MouseAction, ScreenInfoAction

    public class KeyStroke
    extends java.lang.Object
    Represents the user pressing a key on the keyboard. If the user held down ctrl and/or alt before pressing the key, this may be recorded in this class, depending on the terminal implementation and if such information in available. KeyStroke objects are normally constructed by a KeyDecodingProfile, which works off a character stream that likely coming from the system's standard input. Because of this, the class can only represent what can be read and interpreted from the input stream; for example, certain key-combinations like ctrl+i is indistinguishable from a tab key press.

    Use the keyType field to determine what kind of key was pressed. For ordinary letters, numbers and symbols, the keyType will be KeyType.Character and the actual character value of the key is in the character field. Please note that return (\n) and tab (\t) are not sorted under type KeyType.Character but KeyType.Enter and KeyType.Tab instead.

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  KeyStroke.RealF3
      an F3-KeyStroke that is distinguishable from a CursorLocation report.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
        KeyStroke​(KeyType keyType)
      Constructs a KeyStroke based on a supplied keyType; character will be null and both ctrl and alt will be considered not pressed.
        KeyStroke​(KeyType keyType, boolean ctrlDown, boolean altDown)
      Constructs a KeyStroke based on a supplied keyType; character will be null.
        KeyStroke​(KeyType keyType, boolean ctrlDown, boolean altDown, boolean shiftDown)
      Constructs a KeyStroke based on a supplied keyType; character will be null.
      private KeyStroke​(KeyType keyType, java.lang.Character character, boolean ctrlDown, boolean altDown, boolean shiftDown)  
        KeyStroke​(java.lang.Character character, boolean ctrlDown, boolean altDown)
      Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.
        KeyStroke​(java.lang.Character character, boolean ctrlDown, boolean altDown, boolean shiftDown)
      Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(java.lang.Object obj)  
      static KeyStroke fromString​(java.lang.String keyStr)
      Creates a Key from a string representation in Vim's key notation.
      java.lang.Character getCharacter()
      For keystrokes of ordinary keys (letters, digits, symbols), this method returns the actual character value of the key.
      long getEventTime()
      Gets the time when the keystroke was recorded.
      KeyType getKeyType()
      Type of key that was pressed on the keyboard, as represented by the KeyType enum.
      int hashCode()  
      boolean isAltDown()  
      boolean isCtrlDown()  
      boolean isShiftDown()  
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, finalize, getClass, notify, notifyAll, wait, wait, wait
    • Field Detail

      • keyType

        private final KeyType keyType
      • character

        private final java.lang.Character character
      • ctrlDown

        private final boolean ctrlDown
      • altDown

        private final boolean altDown
      • shiftDown

        private final boolean shiftDown
      • eventTime

        private final long eventTime
    • Constructor Detail

      • KeyStroke

        public KeyStroke​(KeyType keyType)
        Constructs a KeyStroke based on a supplied keyType; character will be null and both ctrl and alt will be considered not pressed. If you try to construct a KeyStroke with type KeyType.Character with this constructor, it will always throw an exception; use another overload that allows you to specify the character value instead.
        Parameters:
        keyType - Type of the key pressed by this keystroke
      • KeyStroke

        public KeyStroke​(KeyType keyType,
                         boolean ctrlDown,
                         boolean altDown)
        Constructs a KeyStroke based on a supplied keyType; character will be null. If you try to construct a KeyStroke with type KeyType.Character with this constructor, it will always throw an exception; use another overload that allows you to specify the character value instead.
        Parameters:
        keyType - Type of the key pressed by this keystroke
        ctrlDown - Was ctrl held down when the main key was pressed?
        altDown - Was alt held down when the main key was pressed?
      • KeyStroke

        public KeyStroke​(KeyType keyType,
                         boolean ctrlDown,
                         boolean altDown,
                         boolean shiftDown)
        Constructs a KeyStroke based on a supplied keyType; character will be null. If you try to construct a KeyStroke with type KeyType.Character with this constructor, it will always throw an exception; use another overload that allows you to specify the character value instead.
        Parameters:
        keyType - Type of the key pressed by this keystroke
        ctrlDown - Was ctrl held down when the main key was pressed?
        altDown - Was alt held down when the main key was pressed?
        shiftDown - Was shift held down when the main key was pressed?
      • KeyStroke

        public KeyStroke​(java.lang.Character character,
                         boolean ctrlDown,
                         boolean altDown)
        Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.

        A character-based KeyStroke does not support the shiftDown flag, as the shift state has already been accounted for in the character itself, depending on user's keyboard layout.

        Parameters:
        character - Character that was typed on the keyboard
        ctrlDown - Was ctrl held down when the main key was pressed?
        altDown - Was alt held down when the main key was pressed?
      • KeyStroke

        public KeyStroke​(java.lang.Character character,
                         boolean ctrlDown,
                         boolean altDown,
                         boolean shiftDown)
        Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.

        A character-based KeyStroke does not support the shiftDown flag, as the shift state has already been accounted for in the character itself, depending on user's keyboard layout.

        Parameters:
        character - Character that was typed on the keyboard
        ctrlDown - Was ctrl held down when the main key was pressed?
        altDown - Was alt held down when the main key was pressed?
        shiftDown - Was shift held down when the main key was pressed?
      • KeyStroke

        private KeyStroke​(KeyType keyType,
                          java.lang.Character character,
                          boolean ctrlDown,
                          boolean altDown,
                          boolean shiftDown)
    • Method Detail

      • getKeyType

        public KeyType getKeyType()
        Type of key that was pressed on the keyboard, as represented by the KeyType enum. If the value if KeyType.Character, you need to call getCharacter() to find out which letter, number or symbol that was actually pressed.
        Returns:
        Type of key on the keyboard that was pressed
      • getCharacter

        public java.lang.Character getCharacter()
        For keystrokes of ordinary keys (letters, digits, symbols), this method returns the actual character value of the key. For all other key types, it returns null.
        Returns:
        Character value of the key pressed, or null if it was a special key
      • isCtrlDown

        public boolean isCtrlDown()
        Returns:
        Returns true if ctrl was help down while the key was typed (depending on terminal implementation)
      • isAltDown

        public boolean isAltDown()
        Returns:
        Returns true if alt was help down while the key was typed (depending on terminal implementation)
      • isShiftDown

        public boolean isShiftDown()
        Returns:
        Returns true if shift was help down while the key was typed (depending on terminal implementation)
      • getEventTime

        public long getEventTime()
        Gets the time when the keystroke was recorded. This isn't necessarily the time the keystroke happened, but when Lanterna received the event, so it may not be accurate down to the millisecond.
        Returns:
        The unix time of when the keystroke happened, in milliseconds
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • equals

        public boolean equals​(java.lang.Object obj)
        Overrides:
        equals in class java.lang.Object
      • fromString

        public static KeyStroke fromString​(java.lang.String keyStr)
        Creates a Key from a string representation in Vim's key notation.
        Parameters:
        keyStr - the string representation of this key
        Returns:
        the created KeyType