Module: Tuile::Keys

Defined in:
lib/tuile/keys.rb

Overview

Constants for keys returned by Keys.getkey and helpers for reading them from stdin. The constants are the raw escape sequences emitted by the terminal; see en.wikipedia.org/wiki/ANSI_escape_code for the encoding.

Constant Summary collapse

DOWN_ARROW =

Returns:

  • (String)
"\e[B"
UP_ARROW =

Returns:

  • (String)
"\e[A"
DOWN_ARROWS =

Returns:

  • (Array<String>)
[DOWN_ARROW, "j"].freeze
UP_ARROWS =

Returns:

  • (Array<String>)
[UP_ARROW, "k"].freeze
LEFT_ARROW =

Returns:

  • (String)
"\e[D"
RIGHT_ARROW =

Returns:

  • (String)
"\e[C"
CTRL_LEFT_ARROW =

Returns:

  • (String)
"\e[1;5D"
CTRL_RIGHT_ARROW =

Returns:

  • (String)
"\e[1;5C"
ESC =

Returns:

  • (String)
"\e"
HOME =

Returns:

  • (String)
"\e[H"
END_ =

Returns:

  • (String)
"\e[F"
HOMES =

Home-key sequences. xterm-style (‘HOME`) is the modern default, but the Linux console, rxvt, and tmux/screen in their default configuration emit the VT220-style `e[1~` instead. Components that handle Home should match against this array so users see consistent behavior regardless of which sequence their terminal emits.

Returns:

  • (Array<String>)
[HOME, "\e[1~"].freeze
ENDS_ =

End-key sequences. See HOMES for why two are recognized.

Returns:

  • (Array<String>)
[END_, "\e[4~"].freeze
PAGE_UP =

Returns:

  • (String)
"\e[5~"
PAGE_DOWN =

Returns:

  • (String)
"\e[6~"
BACKSPACE =

Returns:

  • (String)
""
DELETE =

Returns:

  • (String)
"\e[3~"
CTRL_H =

Returns:

  • (String)
"\b"
BACKSPACES =

Returns:

  • (Array<String>)
[BACKSPACE, CTRL_H].freeze
CTRL_U =

Returns:

  • (String)
""
CTRL_D =

Returns:

  • (String)
""
ENTER =

Returns:

  • (String)
"
"
TAB =

Returns:

  • (String)
"\t"
SHIFT_TAB =

The terminal sequence emitted by Shift+Tab in xterm-style terminals (CSI Z). Used by Screen for reverse focus traversal.

Returns:

  • (String)
"\e[Z"

Class Method Summary collapse

Class Method Details

.getkeyString

Grabs a key from stdin and returns it. Blocks until the key is obtained. Reads a full ESC key sequence; see constants above for some values returned by this function.

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/tuile/keys.rb', line 69

def self.getkey
  char = $stdin.getch
  return char unless char == Keys::ESC

  # Escape sequence. Try to read more data.
  begin
    # Read 6 chars: mouse events are e.g. `\e[Mxyz`
    char += $stdin.read_nonblock(6)
  rescue IO::EAGAINWaitReadable
    # The "ESC" key pressed => only the \e char is emitted.
  end
  char
end