Skip to main content
TUI4J translates terminal mouse escape sequences into typed MouseMessage values that your update() method receives like any other message. You choose the reporting mode — cell-level motion, all-motion, or disabled — and the runtime handles the rest.

Enabling mouse support

Return a mouse-enable message from init() to activate reporting when the program starts, or return it from update() to toggle at runtime.
You can also use the convenience methods on Command:
EnableMouseAllMotionMessage reports the cursor position on every mouse move, which lets you implement hover effects. EnableMouseCellMotionMessage only fires while a button is held, which is lighter on bandwidth and enough for drag-selection.

The MouseMessage type

Every mouse event arrives as a MouseMessage with the following fields:

MouseAction

MouseButton

Handling mouse events in update()

Match on MouseMessage in your update() method alongside other message types. The example below tracks drag-selection: it records start coordinates on press, extends the selection during motion, and clears the active flag on release.

Hit testing

Compare mouse.column() and mouse.row() against the bounds of a rendered region to detect hover or click:
You can then change the rendered output based on whether the cursor is inside:
Row and column positions are 0-based after TUI4J normalizes the raw escape sequence coordinates.

Scroll wheel

Scroll events set the button to one of the four wheel values while getAction() is MouseActionPress. Use isWheel() for a quick check:

Disabling mouse on exit

Return DisableMouseMessage before quitting to restore the terminal’s default mouse behavior:

Running the program with mouse support

Pass the mouse mode to Program via the builder methods instead of (or in addition to) returning it from init():
Use withMouseCellMotion() for drag-selection scenarios and withMouseAllMotion() when you need hover effects. All-motion generates significantly more events, so prefer cell-motion when you don’t need hover.

Spring Boot integration

Embed TUI4J in a Spring Boot app

External commands

Launch editors and shells from your TUI