Class: Terminal::KeyEvent

Inherits:
Object
  • Object
show all
Defined in:
lib/terminal/input/key_event.rb

Overview

Key event reported from read_key_event and on_key_event.

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.cachingtrue, false

Returns whether Terminal::KeyEvents should be cached.

Returns:



95
# File 'lib/terminal/input/key_event.rb', line 95

def caching = !!@cache

.key_namesArray<Symbol> (readonly)

Returns list of all avilable key names (see #key).

Returns:

  • (Array<Symbol>)

    list of all avilable key names (see #key)



104
105
106
107
108
109
110
111
112
# File 'lib/terminal/input/key_event.rb', line 104

def key_names
  (
    @names ||=
      (@csiu.values + @csi.values + @ss3.values)
        .uniq
        .keep_if { Symbol === _1 }
        .sort!
  ).dup
end

Instance Attribute Details

#keyString, Symbol (readonly)

Pressed key without any modifiers. This can be a string for simple keys like "a" or a Symbol like :F12 (see key_names).

Returns:

  • (String, Symbol)

    key without modifiers



12
13
14
# File 'lib/terminal/input/key_event.rb', line 12

def key
  @key
end

#modifierInteger (readonly)

Modifier key code. This represents the encoded key modifier like Shift or Alt.

Returns:

  • (Integer)

    modifier key code



18
19
20
# File 'lib/terminal/input/key_event.rb', line 18

def modifier
  @modifier
end

#modifier?true, false (readonly)

Returns whether a key modifier was pressed.

Returns:

  • (true, false)

    whether a key modifier was pressed



22
# File 'lib/terminal/input/key_event.rb', line 22

def modifier? = @modifier != 0

#nameString (readonly)

Name of the key event. This can be a simple name like "a" or "Shift+Ctrl+F12" for combined keys.

Returns:

  • (String)

    key name



29
30
31
# File 'lib/terminal/input/key_event.rb', line 29

def name
  @name
end

#position[Integer,Integer] (readonly)

Mouse event position.

Returns:

  • ([Integer,Integer])

    row and column

  • nil when no coordinates are assigned



35
36
37
# File 'lib/terminal/input/key_event.rb', line 35

def position
  @position
end

#rawString (readonly)

ANSI code sequence received from standard input. This can be a simple value like "a"or more complex input like "\e[24;6~" (for Shift+Ctrl+F12).

Returns:

  • (String)

    received ANSI code sequence



42
43
44
# File 'lib/terminal/input/key_event.rb', line 42

def raw
  @raw
end

#simple?true, false (readonly)

Returns whether a simple key was pressed.

Returns:

  • (true, false)

    whether a simple key was pressed



46
# File 'lib/terminal/input/key_event.rb', line 46

def simple? = @raw == @name

Class Method Details

.[](raw) ⇒ KeyEvent

Translate an ANSI input code sequence into a related KeyEvent.

Parameters:

  • raw (String)

    keyboard, mouse or focus event sequence

Returns:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/terminal/input/key_event.rb', line 118

def [](raw)
  cached = @cache[raw] and return cached if @cache
  return new(raw, *@single_key[raw.ord]) if raw.size == 1
  return unknown(raw) if raw[0] != "\e"
  return esc1(raw, raw[1]) if raw.size == 2 # ESC ?
  case raw[1]
  when "\e"
    return esc_esc(raw) # ESC ESC ...
  when 'O'
    return new(raw, *@ss3[raw[2].ord]) if raw.size == 3 # ESC O ?
  when '['
    return new(raw, *@ss3[raw[2].ord]) if raw.size == 3 # ESC [ ?
    if raw.size == 6 && raw[2] == 'M' # ESC [ M b c r
      return mouse_vt200(raw)
    end
    if raw.size > 4 && raw[2] == '1' && raw[3] == ';'
      return csi(raw) # ESC [ 1 ; ...
    end
    case raw[-1]
    when '~'
      return legacy(raw) # ESC [ ... ~
    when 'u'
      return csi_u(raw) # ESC [ ... u
    when 'M', 'm'
      # ESC [ ... M
      # ESC [ ... m
      return mouse_sgr(raw) if raw[2] == '<'
    end
  end
  unknown(raw)
end

Instance Method Details

#to_aArray<Symbol, String>

All pressed keys. This is composed by all #modifier and the #key.

Examples:

Terminal::KeyEvent["\e\r"].to_a
=> [:Alt, :Enter]
Terminal::KeyEvent['a'].to_a
=> ['a']
Terminal::KeyEvent["\e[<12;45;30m"].to_a
=> [:Shift, :Alt, :MBLeftUp]

Returns:

  • (Array<Symbol, String>)

    all pressed keys



60
# File 'lib/terminal/input/key_event.rb', line 60

def to_a = @ary.dup