|
| 1 | +# mousetrack |
| 2 | + |
| 3 | +`mousetrack` is a small (~5KB) Java 8+ terminal mouse-tracking utility, part of the [java-miniterm](../README.md) project. |
| 4 | + |
| 5 | +It provides static helpers for enabling and disabling terminal mouse-event reporting protocols by writing the appropriate ANSI DEC private-mode sequences to any `Appendable`, and for detecting and parsing the escape sequences that the terminal sends back into structured `MouseEvent` objects. |
| 6 | + |
| 7 | +Three wire formats are supported: legacy X10, SGR (recommended), and URXVT. |
| 8 | +For pixel-accurate coordinates, SGR can be combined with the SGR-Pixels encoding (mode 1016). |
| 9 | + |
| 10 | +## Usage |
| 11 | + |
| 12 | +### Enabling mouse tracking |
| 13 | + |
| 14 | +Choose a [protocol](#protocols) and write the enable sequence to the terminal output. Pair an [encoding](#encodings) with it when you need coordinates beyond column/row 223 — `SGR` is the recommended choice. |
| 15 | + |
| 16 | +```java |
| 17 | +import org.codejive.miniterm.mousetrack.MouseTracking; |
| 18 | + |
| 19 | +MouseTracking.enable(terminal, MouseTracking.Protocol.NORMAL); |
| 20 | +MouseTracking.enableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 21 | +``` |
| 22 | + |
| 23 | +Always disable tracking before exiting — ideally in a `finally` block — so the terminal is left in a clean state: |
| 24 | + |
| 25 | +```java |
| 26 | +try { |
| 27 | + // … read and handle events … |
| 28 | +} finally { |
| 29 | + MouseTracking.disableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 30 | + MouseTracking.disable(terminal, MouseTracking.Protocol.NORMAL); |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +### Detecting and parsing mouse events |
| 35 | + |
| 36 | +After reading an escape sequence (e.g. via `AnsiReader` from the `ansiparser` module), check whether it is a mouse event and decode it: |
| 37 | + |
| 38 | +```java |
| 39 | +import org.codejive.miniterm.mousetrack.MouseEvent; |
| 40 | +import org.codejive.miniterm.mousetrack.MouseTracking; |
| 41 | + |
| 42 | +if (MouseTracking.isMouseEvent(seq)) { |
| 43 | + MouseEvent ev = MouseTracking.parse(seq); |
| 44 | + System.out.printf("%-8s %-12s at (%d, %d)%n", |
| 45 | + ev.type(), ev.button(), ev.x(), ev.y()); |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +`MouseEvent` exposes: |
| 50 | + |
| 51 | +| Method | Description | |
| 52 | +|---|---| |
| 53 | +| `type()` | `PRESS`, `RELEASE`, `MOVE`, `DRAG`, or `SCROLL` | |
| 54 | +| `button()` | `LEFT`, `MIDDLE`, `RIGHT`, `SCROLL_UP`, `SCROLL_DOWN`, or `NONE` | |
| 55 | +| `x()` | 1-based column | |
| 56 | +| `y()` | 1-based row | |
| 57 | +| `shift()` | Shift modifier held | |
| 58 | +| `alt()` | Alt/Meta modifier held | |
| 59 | +| `ctrl()` | Ctrl modifier held | |
| 60 | + |
| 61 | +### Full example with `miniterm` and `ansiparser` |
| 62 | + |
| 63 | +```java |
| 64 | +import org.codejive.miniterm.Terminal; |
| 65 | +import org.codejive.miniterm.ansiparser.AnsiReader; |
| 66 | +import org.codejive.miniterm.mousetrack.MouseEvent; |
| 67 | +import org.codejive.miniterm.mousetrack.MouseTracking; |
| 68 | + |
| 69 | +try (Terminal terminal = Terminal.create()) { |
| 70 | + terminal.enableRawMode(); |
| 71 | + MouseTracking.enable(terminal, MouseTracking.Protocol.BUTTON_MOTION); |
| 72 | + MouseTracking.enableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 73 | + try { |
| 74 | + AnsiReader reader = new AnsiReader(() -> terminal.read(1000)); |
| 75 | + String token; |
| 76 | + while ((token = reader.read()) != null) { |
| 77 | + if (token.isEmpty()) continue; // timeout |
| 78 | + if (!token.startsWith("\u001b") && token.charAt(0) == 3) break; // Ctrl+C |
| 79 | + if (MouseTracking.isMouseEvent(token)) { |
| 80 | + MouseEvent ev = MouseTracking.parse(token); |
| 81 | + System.out.println(ev); |
| 82 | + } |
| 83 | + } |
| 84 | + } finally { |
| 85 | + MouseTracking.disableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 86 | + MouseTracking.disable(terminal, MouseTracking.Protocol.BUTTON_MOTION); |
| 87 | + } |
| 88 | +} |
| 89 | +``` |
| 90 | + |
| 91 | +## Protocols |
| 92 | + |
| 93 | +| Constant | DEC mode | Reports | |
| 94 | +|---|---|---| |
| 95 | +| `Protocol.X10` | `?9` | Button-press events only | |
| 96 | +| `Protocol.NORMAL` | `?1000` | Button press and release | |
| 97 | +| `Protocol.BUTTON_MOTION` | `?1002` | Press, release, and drag (motion while a button is held) | |
| 98 | +| `Protocol.ANY_MOTION` | `?1003` | Press, release, drag, and hover (all mouse movement) | |
| 99 | + |
| 100 | +`ANY_MOTION` can generate a very large number of events; use it only when needed. |
| 101 | + |
| 102 | +## Encodings |
| 103 | + |
| 104 | +| Constant | DEC mode | Coordinate limit | Notes | |
| 105 | +|---|---|---|---| |
| 106 | +| *(default)* | — | 223 cols/rows | Legacy X10 byte encoding; no mode to enable | |
| 107 | +| `Encoding.UTF8` | `?1005` | ~2047 | Deprecated by many terminals | |
| 108 | +| `Encoding.SGR` | `?1006` | Unlimited | **Recommended** — decimal fields, unambiguous release | |
| 109 | +| `Encoding.URXVT` | `?1015` | Unlimited | Decimal fields but does not identify the released button | |
| 110 | +| `Encoding.SGR_PIXELS` | `?1016` | Unlimited | Reports **pixel coordinates** instead of cell coordinates; requires `SGR` to be enabled first | |
| 111 | + |
| 112 | +### SGR-Pixels (pixel-accurate coordinates) |
| 113 | + |
| 114 | +`Encoding.SGR_PIXELS` (DEC mode 1016) is an extension of SGR that makes the terminal report the exact pixel position of the cursor rather than its character-cell position. Enable it together with `Encoding.SGR`: |
| 115 | + |
| 116 | +```java |
| 117 | +MouseTracking.enable(terminal, MouseTracking.Protocol.NORMAL); |
| 118 | +MouseTracking.enableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 119 | +MouseTracking.enableEncoding(terminal, MouseTracking.Encoding.SGR_PIXELS); |
| 120 | +try { |
| 121 | + // ev.x() / ev.y() are now pixel offsets from the top-left corner of the terminal window |
| 122 | +} finally { |
| 123 | + MouseTracking.disableEncoding(terminal, MouseTracking.Encoding.SGR_PIXELS); |
| 124 | + MouseTracking.disableEncoding(terminal, MouseTracking.Encoding.SGR); |
| 125 | + MouseTracking.disable(terminal, MouseTracking.Protocol.NORMAL); |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +Not all terminals support mode 1016; check the terminal's documentation before relying on it. |
| 130 | + |
| 131 | +## Adding the dependency |
| 132 | + |
| 133 | +### JBang |
| 134 | + |
| 135 | +```java |
| 136 | +//DEPS org.codejive.miniterm:mousetrack:0.1.0 |
| 137 | +``` |
| 138 | + |
| 139 | +### Maven |
| 140 | + |
| 141 | +```xml |
| 142 | +<dependency> |
| 143 | + <groupId>org.codejive.miniterm</groupId> |
| 144 | + <artifactId>mousetrack</artifactId> |
| 145 | + <version>0.1.0</version> |
| 146 | +</dependency> |
| 147 | +``` |
| 148 | + |
| 149 | +### Gradle |
| 150 | + |
| 151 | +```kotlin |
| 152 | +implementation("org.codejive.miniterm:mousetrack:0.1.0") |
| 153 | +``` |
| 154 | + |
| 155 | +## Building |
| 156 | + |
| 157 | +```bash |
| 158 | +./mvnw clean install |
| 159 | +``` |
0 commit comments