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
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
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
The X coordinate of the mouse pointer (0-based, terminal columns)
The Y coordinate of the mouse pointer (0-based, terminal rows)
The renderable element that was clicked or is under the cursor
The source renderable for drag-and-drop operations (available in
"drop" and "over" events during drag)Keyboard modifiers held during the mouse event
Scroll information (only present for
"scroll" events)Whether this event is part of an active drag operation
Whether
preventDefault() was called on this eventWhether
stopPropagation() was called on this eventMethods
Prevents the default action for this mouse event from executing (e.g., prevents focus change on click)
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:Fired when a mouse button is pressed while over the renderableCallback arguments:
event(MouseEvent)
Fired when a mouse button is releasedCallback arguments:
event(MouseEvent)
Fired when the mouse moves over the renderable without any buttons pressedCallback arguments:
event(MouseEvent)
Fired when the mouse moves with a button held downCallback arguments:
event(MouseEvent)
Fired when a drag operation endsCallback arguments:
event(MouseEvent)
Fired when a dragged item is dropped on the renderableCallback arguments:
event(MouseEvent) -event.sourcecontains the dragged renderable
Fired when the mouse enters the renderable’s boundsCallback arguments:
event(MouseEvent)
Fired when the mouse leaves the renderable’s boundsCallback arguments:
event(MouseEvent)
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
Enable or disable mouse input handling globally
Track mouse movement and hover events (may reduce performance in some terminals)
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)