Class: Terminal::KeyEvent

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

Overview

Key event reported from read_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 KeyCodes should be cached.

Returns:

  • (true, false)

    whether KeyCodes should be cached



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

def caching = !!@cache

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.

Returns:

  • (String, Symbol)

    key without modifiers



158
159
160
# File 'lib/terminal/input/key_event.rb', line 158

def key
  @key
end

#modifierInteger (readonly)

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

Returns:

  • (Integer)

    modifier key code



164
165
166
# File 'lib/terminal/input/key_event.rb', line 164

def modifier
  @modifier
end

#modifier?true, false (readonly)

Returns whether a key modifier was pressed.

Returns:

  • (true, false)

    whether a key modifier was pressed



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

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



175
176
177
# File 'lib/terminal/input/key_event.rb', line 175

def name
  @name
end

#rawString (readonly)

Event string 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 event string



153
154
155
# File 'lib/terminal/input/key_event.rb', line 153

def raw
  @raw
end

#simple?true, false (readonly)

Returns whether a simple key was pressed.

Returns:

  • (true, false)

    whether a simple key was pressed



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

def simple? = @raw == @name

Class Method Details

.[](raw) ⇒ KeyEvent

Translate a keyboard input string into a related KeyEvent

Parameters:

  • raw (String)

    keyboard input

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/terminal/input/key_event.rb', line 26

def [](raw)
  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" # ESC ESC ...
    return esc_esc(raw)
  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
    return csi1(raw) if raw.start_with?("\e[1;") # ESC [ 1 ; ...
    case raw[-1]
    when '~' # ESC [ ... ~
      return legacy(raw)
    when 'u' # ESC [ ... u
      return csi_u(raw)
    when 'M' # ESC [ ... M
      return raw[2] == '<' ? mouse_sgr(raw) : mouse_urxvt(raw)
    when '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.

Returns:

  • (Array<Symbol, String>)

    all pressed keys



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

def to_a = @ary.dup