Class: Terminal::KeyEvent
- Inherits:
-
Object
- Object
- Terminal::KeyEvent
- Defined in:
- lib/terminal/input/key_event.rb
Overview
Key event reported from read_key_event and on_key_event.
Class Attribute Summary collapse
-
.caching ⇒ true, false
Whether KeyEvents should be cached.
-
.key_names ⇒ Array<Symbol>
readonly
List of all avilable key names (see #key).
Instance Attribute Summary collapse
-
#key ⇒ String, Symbol
readonly
Pressed key without any modifiers.
-
#modifier ⇒ Integer
readonly
Modifier key code.
-
#modifier? ⇒ true, false
readonly
Whether a key modifier was pressed.
-
#name ⇒ String
readonly
Name of the key event.
-
#position ⇒ [Integer,Integer]
readonly
Mouse event position.
-
#raw ⇒ String
readonly
ANSI code sequence received from standard input.
-
#simple? ⇒ true, false
readonly
Whether a simple key was pressed.
Class Method Summary collapse
-
.[](raw) ⇒ KeyEvent
Translate an ANSI input code sequence into a related KeyEvent.
Instance Method Summary collapse
-
#to_a ⇒ Array<Symbol, String>
All pressed keys.
Class Attribute Details
.caching ⇒ true, false
Returns whether Terminal::KeyEvents should be cached.
95 |
# File 'lib/terminal/input/key_event.rb', line 95 def caching = !!@cache |
.key_names ⇒ Array<Symbol> (readonly)
Returns 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
#key ⇒ String, 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).
12 13 14 |
# File 'lib/terminal/input/key_event.rb', line 12 def key @key end |
#modifier ⇒ Integer (readonly)
Modifier key code. This represents the encoded key modifier like Shift
or Alt.
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.
22 |
# File 'lib/terminal/input/key_event.rb', line 22 def modifier? = @modifier != 0 |
#name ⇒ String (readonly)
Name of the key event.
This can be a simple name like "a" or "Shift+Ctrl+F12" for combined
keys.
29 30 31 |
# File 'lib/terminal/input/key_event.rb', line 29 def name @name end |
#position ⇒ [Integer,Integer] (readonly)
Mouse event position.
35 36 37 |
# File 'lib/terminal/input/key_event.rb', line 35 def position @position end |
#raw ⇒ String (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).
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.
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.
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 |