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"
ESC =

Returns:

  • (String)
"\e"
HOME =

Returns:

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

Returns:

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

Returns:

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

Returns:

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

Returns:

  • (String)
"\u007f"
DELETE =

Returns:

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

Returns:

  • (String)
"\b"
BACKSPACES =

Returns:

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

Returns:

  • (String)
"\u0015"
CTRL_D =

Returns:

  • (String)
"\u0004"
ENTER =

Returns:

  • (String)
"\u000d"

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:



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/tuile/keys.rb', line 49

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