Class: TTY::Prompt::Reader::KeyEvent

Inherits:
Struct
  • Object
show all
Defined in:
lib/tty/prompt/reader/key_event.rb

Overview

Represents key event emitted during keyboard press

API:

  • public

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • the current value of key



18
19
20
# File 'lib/tty/prompt/reader/key_event.rb', line 18

def key
  @key
end

#valueObject

Returns the value of attribute value

Returns:

  • the current value of value



18
19
20
# File 'lib/tty/prompt/reader/key_event.rb', line 18

def value
  @value
end

Class Method Details

.from(keys, char) ⇒ KeyEvent

Create key event from read input codes

Parameters:

  • the keys and codes mapping

Returns:

API:

  • public



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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/tty/prompt/reader/key_event.rb', line 28

def self.from(keys, char)
  key = Key.new
  ctrls = keys.keys.grep(/ctrl/)

  case char
  when keys[:return] then key.name = :return
  when keys[:enter]  then key.name = :enter
  when keys[:tab]    then key.name = :tab
  when keys[:backspace] then key.name = :backspace
  when keys[:delete] then key.name = :delete
  when keys[:space]  then key.name = :space
  when keys[:escape] then key.name = :escape
  when proc { |c| c =~ /^[a-z]{1}$/ }
    key.name = :alpha
  when proc { |c| c =~ /^[A-Z]{1}$/ }
    key.name = :alpha
    key.shift = true
  when proc { |c| c =~ /^\d+$/ }
    key.name = :num
  # arrows
  when keys[:up]    then key.name = :up
  when keys[:down]  then key.name = :down
  when keys[:left]  then key.name = :left
  when keys[:right] then key.name = :right
  # editing
  when keys[:clear] then key.name = :clear
  when keys[:end]   then key.name = :end
  when keys[:home]  then key.name = :home
  when keys[:insert]    then key.name = :insert
  when keys[:page_up]   then key.name = :page_up
  when keys[:page_down] then key.name = :page_down
  when proc { |cs| ctrls.any? { |name| keys[name] == cs } }
    key.name = keys.key(char)
    key.ctrl = true
  # f1 - f12
  when keys[:f1], keys[:f1_xterm] then key.name = :f1
  when keys[:f2], keys[:f2_xterm] then key.name = :f2
  when keys[:f3], keys[:f3_xterm] then key.name = :f3
  when keys[:f4], keys[:f4_xterm] then key.name = :f4
  when keys[:f5] then key.name = :f5
  when keys[:f6] then key.name = :f6
  when keys[:f7] then key.name = :f7
  when keys[:f8] then key.name = :f8
  when keys[:f9] then key.name = :f9
  when keys[:f10] then key.name = :f10
  when keys[:f11] then key.name = :f11
  when keys[:f12] then key.name = :f12
  end

  new(char, key)
end

Instance Method Details

#trigger?Boolean

Check if key event can be triggered

Returns:

API:

  • public



85
86
87
# File 'lib/tty/prompt/reader/key_event.rb', line 85

def trigger?
  !key.nil? && !key.name.nil?
end