Skip to main content

Overview

OpenTUI provides comprehensive mouse input handling for terminal user interfaces, supporting clicks, drags, scrolling, and hover events.

MouseEvent

The MouseEvent class represents a mouse interaction event with position, button information, and modifiers.

Properties

MouseEventType
The type of mouse event:
  • "down" - Mouse button pressed
  • "up" - Mouse button released
  • "move" - Mouse moved without buttons pressed
  • "drag" - Mouse moved with button pressed
  • "drag-end" - Drag operation ended
  • "drop" - Item dropped on target
  • "over" - Mouse entered element
  • "out" - Mouse left element
  • "scroll" - Mouse wheel scrolled
number
The mouse button that triggered the event:
  • 0 (MouseButton.LEFT) - Left button
  • 1 (MouseButton.MIDDLE) - Middle button
  • 2 (MouseButton.RIGHT) - Right button
  • 4 (MouseButton.WHEEL_UP) - Scroll wheel up
  • 5 (MouseButton.WHEEL_DOWN) - Scroll wheel down
number
The X coordinate of the mouse pointer (0-based, terminal columns)
number
The Y coordinate of the mouse pointer (0-based, terminal rows)
Renderable | null
The renderable element that was clicked or is under the cursor
Renderable
The source renderable for drag-and-drop operations (available in "drop" and "over" events during drag)
object
Keyboard modifiers held during the mouse event
ScrollInfo
Scroll information (only present for "scroll" events)
boolean
Whether this event is part of an active drag operation
boolean
Whether preventDefault() was called on this event
boolean
Whether stopPropagation() was called on this event

Methods

() => void
Prevents the default action for this mouse event from executing (e.g., prevents focus change on click)
() => void
Stops the event from propagating to parent elements

Example

MouseButton Enum

The MouseButton enum provides constants for mouse button identification:

Example

Mouse Events on Renderables

Renderables can listen to mouse events using the following event names:
event
Fired when a mouse button is pressed while over the renderableCallback arguments:
  • event (MouseEvent)
event
Fired when a mouse button is releasedCallback arguments:
  • event (MouseEvent)
event
Fired when the mouse moves over the renderable without any buttons pressedCallback arguments:
  • event (MouseEvent)
event
Fired when the mouse moves with a button held downCallback arguments:
  • event (MouseEvent)
event
Fired when a drag operation endsCallback arguments:
  • event (MouseEvent)
event
Fired when a dragged item is dropped on the renderableCallback arguments:
  • event (MouseEvent) - event.source contains the dragged renderable
event
Fired when the mouse enters the renderable’s boundsCallback arguments:
  • event (MouseEvent)
event
Fired when the mouse leaves the renderable’s boundsCallback arguments:
  • event (MouseEvent)
event
Fired when the mouse wheel is scrolled over the renderableCallback arguments:
  • event (MouseEvent) - event.scroll contains direction and delta

Renderer Configuration

Mouse input can be configured when creating the renderer:

Options

boolean
default:"true"
Enable or disable mouse input handling globally
boolean
default:"true"
Track mouse movement and hover events (may reduce performance in some terminals)
boolean
default:"true"
Automatically focus renderable elements when clicked with the left mouse button

Mouse Pointer Styles

You can change the mouse cursor style using the renderer:
Available cursor styles depend on terminal support.

Drag and Drop

OpenTUI automatically handles drag and drop when you click and drag on a renderable:

Scroll Handling

Handle mouse wheel scrolling:

Hit Testing

The renderer provides a hitTest method to determine which renderable is at a specific position:

Best Practices

Always call event.preventDefault() when you handle a click to prevent unwanted side effects:
When a child element handles a mouse event, stop it from propagating to the parent:
Provide visual feedback when users hover over interactive elements:
Use keyboard modifiers to provide different behaviors:

Terminal Compatibility

Most modern terminals support mouse input, including:
  • iTerm2 (macOS)
  • Windows Terminal
  • Alacritty
  • Kitty
  • GNOME Terminal
  • Konsole
  • tmux (with mouse mode enabled)
Some legacy terminals may have limited or no mouse support.