Class: TTY::Reader::KeyEvent

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

Overview

Represents key event emitted during keyboard press

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#keyObject

Returns the value of attribute key

Returns:

  • (Object)

    the current value of key



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

def key
  @key
end

#lineObject

Returns the value of attribute line

Returns:

  • (Object)

    the current value of line



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

def line
  @line
end

#valueObject

Returns the value of attribute value

Returns:

  • (Object)

    the current value of value



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

def value
  @value
end

Class Method Details

.from(keys, char, line = "") ⇒ KeyEvent

Create key event from read input codes

Parameters:

  • keys (Hash[Symbol])

    the keys and codes mapping

  • codes (Array[Integer])

Returns:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tty/reader/key_event.rb', line 29

def self.from(keys, char, line = "")
  key = Key.new
  key.name = (name = keys[char]) ? name : :ignore

  case char
  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
  when proc { |cs| !Keys.ctrl_keys[cs].nil? }
    key.ctrl = true
  end

  new(key, char, line)
end

Instance Method Details

#trigger?Boolean

Check if key event can be triggered

Returns:

  • (Boolean)


53
54
55
# File 'lib/tty/reader/key_event.rb', line 53

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