Class: RubyTUI::KeyEvent

Inherits:
Event
  • Object
show all
Defined in:
lib/rubytui/event.rb

Overview

A keyboard event: key code, optional character and modifier flags.

Examples:

event = KeyEvent.new(code: Input::Key::CHAR, char: "q", modifiers: Input::Key::CTRL)
event.ctrl?  # => true
event.to_s   # => "KeyEvent(Ctrl+q)"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code:, char: nil, modifiers: Input::Key::NONE) ⇒ KeyEvent

Returns a new instance of KeyEvent.

Parameters:



23
24
25
26
27
# File 'lib/rubytui/event.rb', line 23

def initialize(code:, char: nil, modifiers: Input::Key::NONE)
  @code = code
  @char = char
  @modifiers = modifiers
end

Instance Attribute Details

#char ⇒ Object (readonly)

Returns the value of attribute char.



16
17
18
# File 'lib/rubytui/event.rb', line 16

def char
  @char
end

#code ⇒ Object (readonly)

Returns the value of attribute code.



16
17
18
# File 'lib/rubytui/event.rb', line 16

def code
  @code
end

#modifiers ⇒ Object (readonly)

Returns the value of attribute modifiers.



16
17
18
# File 'lib/rubytui/event.rb', line 16

def modifiers
  @modifiers
end

Instance Method Details

#==(other) ⇒ Boolean

Value equality: same code, character and modifiers.

Parameters:

  • other (Object) —

    object to compare

Returns:



41
42
43
44
# File 'lib/rubytui/event.rb', line 41

def ==(other)
  other.is_a?(KeyEvent) && @code == other.code &&
    @char == other.char && @modifiers == other.modifiers
end

#alt? ⇒ Boolean

Returns true if the Input::Key::ALT flag is set.

Returns:



32
33
# File 'lib/rubytui/event.rb', line 32

def alt?   = (@modifiers & Input::Key::ALT) != 0
# @return [Boolean] true if the {Input::Key::SHIFT} flag is set

#ctrl? ⇒ Boolean

Returns true if the Input::Key::CTRL flag is set.

Returns:



30
31
# File 'lib/rubytui/event.rb', line 30

def ctrl?  = (@modifiers & Input::Key::CTRL) != 0
# @return [Boolean] true if the {Input::Key::ALT} flag is set

#shift? ⇒ Boolean

Returns true if the Input::Key::SHIFT flag is set.

Returns:



34
# File 'lib/rubytui/event.rb', line 34

def shift? = (@modifiers & Input::Key::SHIFT) != 0

#to_s ⇒ String Also known as: inspect

Human-readable form: modifier names, then the character (or the key code when there is no character), joined by +.

Returns:

  • (String) —

    e.g. KeyEvent(Ctrl+q) or KeyEvent(enter)



50
51
52
53
54
55
56
57
# File 'lib/rubytui/event.rb', line 50

def to_s
  parts = []
  parts << "Ctrl" if ctrl?
  parts << "Alt" if alt?
  parts << "Shift" if shift?
  parts << (@char || @code.to_s)
  "KeyEvent(#{parts.join("+")})"
end