Class KeyStroke

java.lang.Object
com.googlecode.lanterna.input.KeyStroke
Direct Known Subclasses:
KeyStroke.RealF3, MouseAction, ScreenInfoAction

public class KeyStroke extends 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 
    an F3-KeyStroke that is distinguishable from a CursorLocation report.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final boolean
     
    private final Character
     
    private final boolean
     
    private final long
     
    private final KeyType
     
    private final boolean
     
  • 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, Character character, boolean ctrlDown, boolean altDown, boolean shiftDown)
     
     
    KeyStroke(Character character, boolean ctrlDown, boolean altDown)
    Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.
     
    KeyStroke(Character character, boolean ctrlDown, boolean altDown, boolean shiftDown)
    Constructs a KeyStroke based on a supplied character, keyType is implicitly KeyType.Character.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    static KeyStroke
    Creates a Key from a string representation in Vim's key notation.
    For keystrokes of ordinary keys (letters, digits, symbols), this method returns the actual character value of the key.
    long
    Gets the time when the keystroke was recorded.
    Type of key that was pressed on the keyboard, as represented by the KeyType enum.
    int
     
    boolean
     
    boolean
     
    boolean
     
     

    Methods inherited from class java.lang.Object

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

    • keyType

      private final KeyType keyType
    • character

      private final Character character
    • ctrlDown

      private final boolean ctrlDown
    • altDown

      private final boolean altDown
    • shiftDown

      private final boolean shiftDown
    • eventTime

      private final long eventTime
  • Constructor Details

    • 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(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(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, Character character, boolean ctrlDown, boolean altDown, boolean shiftDown)
  • Method Details

    • 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 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 String toString()
      Overrides:
      toString in class Object
    • hashCode

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

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • fromString

      public static KeyStroke fromString(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