Overview
OpenTUI provides comprehensive mouse input handling for terminal user interfaces, supporting clicks, drags, scrolling, and hover events.MouseEvent
TheMouseEvent 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 button1(MouseButton.MIDDLE) - Middle button2(MouseButton.RIGHT) - Right button4(MouseButton.WHEEL_UP) - Scroll wheel up5(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 eventboolean
Whether
stopPropagation() was called on this eventMethods
() => 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
TheMouseButton 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.sourcecontains 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.scrollcontains 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: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 ahitTest method to determine which renderable is at a specific position:
Best Practices
Prevent default behavior when handling clicks
Prevent default behavior when handling clicks
Always call
event.preventDefault() when you handle a click to prevent unwanted side effects:Stop propagation for child elements
Stop propagation for child elements
When a child element handles a mouse event, stop it from propagating to the parent:
Use hover events for visual feedback
Use hover events for visual feedback
Provide visual feedback when users hover over interactive elements:
Check modifiers for advanced interactions
Check modifiers for advanced interactions
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)