class RatatuiRuby::Event::Mouse
Reports a mouse interaction.
Modern terminals support rich pointer input, but the protocols are complex and varied. Handling clicks, drags, and scrolls requires robust parsing.
This event simplifies the complexity. It tells you exactly what happened (kind), where it happened (x, y), and which button was involved.
Use this to build interactive UIs. Implement click handlers, draggable sliders, or scrollable viewports with confidence.
Example
if event.mouse? && event.down? && event.button == "left" puts "Left click at #{event.x}, #{event.y}" end
Attributes
The kind of event ("down", "up", "drag", "moved", "scroll_up", "scroll_down").
puts event.kind # => "down"
List of active modifiers.
puts event.modifiers # => ["ctrl"]
X coordinate (column).
puts event.x # => 10
Y coordinate (row).
puts event.y # => 5
Public Class Methods
Source
# File lib/ratatui_ruby/event/mouse.rb, line 95 def initialize(kind:, x:, y:, button:, modifiers: []) @kind = kind.freeze @x = x @y = y @button = (button || "none").freeze @modifiers = modifiers.map(&:freeze).sort.freeze end
Public Instance Methods
Source
# File lib/ratatui_ruby/event/mouse.rb, line 249 def ==(other) case other when Symbol then to_sym == other when Mouse then kind == other.kind && x == other.x && y == other.y && button == other.button && modifiers == other.modifiers else false end end
Source
# File lib/ratatui_ruby/event/mouse.rb, line 176 def deconstruct_keys(keys) { type: :mouse, kind: @kind, x: @x, y: @y, button: @button, modifiers: @modifiers } end
Deconstructs the event for pattern matching.
case event in type: :mouse, kind: "down", x:, y: puts "Click at #{x}, #{y}" end
Source
# File lib/ratatui_ruby/event/mouse.rb, line 104 def down? @kind == "down" end
Returns true if mouse button was pressed down.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 117 def drag? @kind == "drag" end
Returns true if mouse is being dragged.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 144 def left? @button == "left" end
Returns true if event involves the left mouse button.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 154 def middle? @button == "middle" end
Returns true if event involves the middle mouse button.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 79 def mouse? true end
Returns true for Mouse events.
event.mouse? # => true event.key? # => false event.resize? # => false
Source
# File lib/ratatui_ruby/event/mouse.rb, line 276 def moved? @kind == "moved" end
Returns true for mouse movement without button press.
event.moved? # => true for moved (no button)
Source
# File lib/ratatui_ruby/event/mouse.rb, line 149 def right? @button == "right" end
Returns true if event involves the right mouse button.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 263 def scroll? scroll_up? || scroll_down? end
Returns true for any scroll event.
event.scroll? # => true for scroll_up or scroll_down
Source
# File lib/ratatui_ruby/event/mouse.rb, line 139 def scroll_down? @kind == "scroll_down" end
Returns true if scroll wheel moved down.
if event.scroll_down? scroll_offset += 1 end
Source
# File lib/ratatui_ruby/event/mouse.rb, line 122 def scroll_up? @kind == "scroll_up" end
Returns true if scroll wheel moved up.
Source
# File lib/ratatui_ruby/event/mouse.rb, line 219 def to_sym if @kind.start_with?("scroll") @kind.to_sym elsif @button == "none" :"mouse_#{@kind}" else :"mouse_#{@button}_#{@kind}" end end
Converts the event to a Symbol representation.
The format varies by event type:
- Left Button
-
:mouse_left_down,:mouse_left_up,:mouse_left_drag - Right Button
-
:mouse_right_down,:mouse_right_up,:mouse_right_drag - Middle Button
-
:mouse_middle_down,:mouse_middle_up,:mouse_middle_drag - Scroll
-
:scroll_up,:scroll_down - Move
-
:mouse_moved
Example
event = Event::Mouse.new(kind: "down", x: 10, y: 5, button: "left") event.to_sym # => :mouse_left_down scroll = Event::Mouse.new(kind: "scroll_up", x: 0, y: 0, button: "none") scroll.to_sym # => :scroll_up
Source
# File lib/ratatui_ruby/event/mouse.rb, line 109 def up? @kind == "up" end
Returns true if mouse button was released.