class RatatuiRuby::Event

Base class for all RatatuiRuby events.

Events represent terminal input: keyboard, mouse, resize, paste, focus changes. Returned by RatatuiRuby.poll_event. All events support Ruby 3.0+ pattern matching.

Event Types

Pattern Matching (Exhaustive)

Use case...in to dispatch on every possible event type. This ensures you handle every case without needing an else clause:

case RatatuiRuby.poll_event
in { type: :key, code: "q" }
  break
in { type: :key, code: code, modifiers: }
  handle_key(code, modifiers)
in { type: :mouse, kind: "down", x:, y: }
  handle_click(x, y)
in { type: :mouse, kind:, x:, y: }
  # handle other mouse activities
in { type: :resize, width:, height: }
  handle_resize(width, height)
in { type: :paste, content: }
  handle_paste(content)
in { type: :focus_gained }
  handle_focus_gain
in { type: :focus_lost }
  handle_focus_loss
in { type: :none }
  # Idle
end

Predicates

Check event types with predicates without pattern matching:

event = RatatuiRuby.poll_event
if event.key?
  puts "Key pressed"
elsif event.none?
  # Idle
elsif event.mouse?
  puts "Mouse event"
end