Raxol.Terminal.Input.MouseHandler (Raxol v2.0.1)
View SourceComprehensive mouse event handling for terminal applications.
Supports multiple mouse protocols:
- X10: Original mouse tracking (button press only)
- X11: Mouse tracking with button release
- SGR: Extended mouse protocol with precise coordinates
- URXVT: Extended protocol variant
Features
- Button press/release detection
- Mouse movement tracking
- Scroll wheel support
- Drag operations
- Multi-button chord detection
- High-precision coordinate reporting (SGR mode)
Usage
# Parse a mouse event sequence
MouseHandler.parse_mouse_event("[<0;10;20M")
{:ok, %{type: :press, button: :left, x: 10, y: 20}}
# Enable mouse tracking
MouseHandler.enable_mouse_tracking(:sgr)
# Handle mouse event
MouseHandler.handle_event(state, event)
Summary
Functions
Returns the optimal mouse mode for the current terminal.
Disables all mouse tracking modes.
Enables mouse tracking with the specified mode.
Processes a mouse event and updates the handler state.
Creates a new mouse handler state.
Parses a mouse event sequence from terminal input.
Generates control sequences to set mouse reporting modes.
Types
@type button() ::
:left
| :middle
| :right
| :wheel_up
| :wheel_down
| :button4
| :button5
| :button6
| :button7
| :button8
| :button9
| :button10
| :button11
@type event_type() :: :press | :release | :move | :drag | :scroll
@type mouse_event() :: %{ type: event_type(), button: button() | nil, x: non_neg_integer(), y: non_neg_integer(), modifiers: map(), protocol: atom(), timestamp: non_neg_integer() }
@type mouse_mode() :: :off | :x10 | :x11 | :button_event | :any_event | :sgr | :urxvt
@type state() :: %{ mode: mouse_mode(), pressed_buttons: MapSet.t(button()), last_position: {non_neg_integer(), non_neg_integer()} | nil, drag_start: {non_neg_integer(), non_neg_integer()} | nil, click_count: non_neg_integer(), last_click_time: non_neg_integer() | nil, double_click_threshold: non_neg_integer() }
Functions
@spec detect_best_mode() :: mouse_mode()
Returns the optimal mouse mode for the current terminal.
Detects terminal capabilities and returns the best supported mode.
@spec disable_mouse_tracking() :: binary()
Disables all mouse tracking modes.
@spec enable_mouse_tracking(mouse_mode()) :: binary()
Enables mouse tracking with the specified mode.
Returns the escape sequence to send to the terminal.
@spec handle_event(state(), mouse_event()) :: {state(), list()}
Processes a mouse event and updates the handler state.
Tracks button states, detects drag operations, and counts clicks.
Creates a new mouse handler state.
@spec parse_mouse_event(binary()) :: {:ok, mouse_event()} | {:error, term()}
Parses a mouse event sequence from terminal input.
Automatically detects the protocol used and returns a parsed event.
@spec set_mouse_mode(mouse_mode(), boolean()) :: binary()
Generates control sequences to set mouse reporting modes.