# Input

A surface reports user interactions as events, but only for the events your
program explicitly requests. Most interactions stay local to the terminal—typing
in a text field, toggling a checkbox, hovering, and scrolling. The host notifies
your program only when needed, such as when a user commits a field value.

```text
host → ESC ] 7279 ; a=ev:s=card:e=click:t=go ; {"area": {"c": 0, "r": 0, "w": 4, "h": 1}} ST
```

Events arrive on the program's stdin alongside regular keyboard input
([§9](https://hotty.neuroplast.io/docs/protocol#9-events)). `s` identifies the surface, `e` is the event
type, and `t` is the target element's `id`. The payload is MessagePack (shown
decoded in these examples).

Because `id` is how your program identifies elements, assign an `id` to every
element whose events you want to receive; a button without an `id` does not
report clicks. Use ASCII for IDs, because hosts normalize non-ASCII characters
(for example, `café` is reported as `caf_`) ([§3.2](https://hotty.neuroplast.io/docs/protocol#32-control)).
See [Keyboard and focus](https://hotty.neuroplast.io/docs/concepts/keyboard) for key routing, `focus`, and `blur`.

## What asks for each event

Element-level events are enabled on individual elements, while surface-level
events are enabled on the placement. Unrequested events are never sent:

| event | enabled by | frequency |
| --- | --- | --- |
| `click`, `change`, `input`, `submit` | element type, or `click` / `input` in `data-on` | on every occurrence |
| `caret` | `caret` in a text field's `data-on` | on selection change, at most once per frame |
| `press` | `p=1` on the placement | on every press |
| `dragstart`, `drag`, `dragend` | `drag` in `data-on` (and `drop` for drop targets) | per element, cell, or step crossed |
| `gesturestart`, `gesture`, `gestureend` | `gesture` in `data-on` ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#gestures)) | `gesture` at most once per frame |
| `wheel` | `wheel` in `data-on` ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#the-wheel)) | summed, at most once per frame |
| `contextmenu` | `contextmenu` in `data-on` | on each context-menu gesture: a right click, a long press, a pen's barrel button |
| `auxclick` | `aux` in `data-on` (`rightbutton` for the secondary button) | on each release where the button was pressed |
| `hover` | `v=1` on the placement | on target change, at most once per frame |
| `view` | `view` in `data-on` | once scrolling settles, otherwise at most once per frame |
| `fit` | `f=1` on the placement | at most once per frame |
| `focus`, `blur` | always enabled (keyboard state is always reported) | on every change |
| `command` | `a=edit`, or a named item in the program's menu | on each matching key or menu selection |
| `caps` | capability query | on each capability change |
| `pointer` | capability query | when the user switches to another kind of pointer ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#the-pointer-event)) |

Events are opt-in because they are written to the terminal's input stream. If a
document remained active after its program exited, clicks or edits would inject
escape sequences into the shell. Programs must detach surfaces they leave on
screen ([§5.5](https://hotty.neuroplast.io/docs/protocol#55-ownership-adetach)); detached surfaces emit no
events.

`data-on` accepts a space-separated list of tokens, such as
`data-on="click drag"`. The placement flags `p`, `f`, and `v` apply to the
entire surface: `1` enables reporting, `0` disables it, and the setting persists
until changed ([§5.2](https://hotty.neuroplast.io/docs/protocol#52-placement-aplace)). `fit` notifies your
program when the document's required row count changes, and `caps` notifies it
when host capabilities change.

Pointer and layout events require a visible surface. A hidden surface or one
scrolled out of view does not emit `press`, drag events, `hover`, `fit`, or
`view`. When the surface becomes visible again, the host reports any pending
changes.

`click`, `press`, drag, `gesturestart`, and `wheel` events include `mods`, a
bitmask of held modifier keys: `1` (`shift`), `2` (`alt`), `4` (`ctrl`), and
`8` (`super`). `mods` is omitted when no modifiers are held. The same events,
but for `drag` and `dragend`, carry `pointer`, the kind of pointer that made
them: `"mouse"`, `"pen"`, or `"touch"`
([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#the-kind-of-pointer)). Ignore unrecognized
event types and fields, as future protocol versions may add new ones.

## Clicks

The host emits `click` when the user activates a `<button>`, a link, a
`<summary>`, or an `<input>` of type `button`, `submit`, or `reset`. Other
elements opt in with `data-on="click"`. The event targets the nearest matching
element walking outward from the clicked point.

```html
<button id=save value=draft>Save</button>
<li id=row-7 data-on=click>…</li>
```

```text
host → ESC ] 7279 ; a=ev:s=editor:e=click:t=save ; {"area": {"c": 2, "r": 5, "w": 6, "h": 1}, "value": "draft"} ST
```

- **`area`** is the element's bounding box in cells, zero-indexed from the
  surface's top-left corner. Use it to anchor popups or menus next to the
  element. If the element is partially scrolled out of view, `area` still
  reports its full bounds, so `c` and `r` may be negative.
- **`value`** is included if the element has a `value` attribute.
- **`count`** tracks multi-click runs: `2` for a double click, `3` for a triple
  click. Every click in the sequence is emitted, so a double click produces a
  `click` followed by a `click` with `"count": 2`.

Keyboard activation (`space` or `enter` on a focused button) also emits `click`
with `area`, always as a single click. Only hosts advertising the `count`
feature report click counts, using the user's system double-click settings
([§4](https://hotty.neuroplast.io/docs/protocol#4-detection-and-capabilities)). If `count` is unavailable,
provide an alternative trigger for double-click actions.

## Links

Clicking a link emits a normal `click` event; surfaces never navigate on their
own. Because the link's `href` identifies it, `t` is omitted. The payload
includes the raw `href` attribute and `url`, the address resolved against the
document's base URL ([§7.3](https://hotty.neuroplast.io/docs/protocol#73-the-base-url)):

```text
host → ESC ] 7279 ; a=ev:s=post:e=click ; {"area": {"c": 12, "r": 3, "w": 5, "h": 1}, "href": "../about", "url": "https://example.com/about"} ST
```

Your program decides how to handle the click—for example, rendering another view
or opening the URL with the OS. `url` is omitted if resolution falls under
`https://hotty.invalid/`, which is the default base URL when a document has no
`<base href>`. In that case, relative links include only `href`.

A link with `target="_blank"` is treated as a terminal **hyperlink**, equivalent
to an OSC 8 hyperlink in terminal cells. The host handles it directly using the
terminal's configured gesture, visual feedback, and security settings.

Terminal hyperlinks do not emit `click` events, nor `press` events when the host
intercepts the gesture. A `target="_blank"` link without a resolved `url` is not
treated as a hyperlink.

## Forms

Form controls are handled locally by the host, and your program receives their
values as they commit. Toggling checkboxes, selecting options, and typing text
require no round-trips. Each control emits events according to its type:

| event | when | detail |
| --- | --- | --- |
| `change` | immediately when a checkbox or radio button is toggled or a `<select>` option is picked; for text fields, on `enter` in an `<input>` or on blur if the value changed | `{"checked": bool, "value": str, "rev": int}` for checkboxes and radio buttons; `{"value": str, "rev": int}` otherwise |
| `input` | on every edit to a control with `data-on="input"` | `{"value": str, "rev": int}`, plus `"sel": [int, int]` if `data-on="input caret"` is set |
| `caret` | when the user moves the selection in a text field with `data-on="caret"` and no `input` event was emitted | `{"sel": [int, int], "rev": int}` |
| `submit` | when a submit button is activated or `enter` is pressed in a text field (emitted after `change`) | `{"fields": [[str, str]]}`: each named control's name and value in document order |

Here is a search input that streams every keystroke. As the user types `"ad"`
and presses `enter`, the host emits two `input` events, commits the field with
`change`, and submits the form:

```html
<form id=search>
  <input id=q name=q data-on=input>
</form>
```

```text
host → ESC ] 7279 ; a=ev:s=find:e=input:t=q ; {"value": "a", "rev": 1} ST
host → ESC ] 7279 ; a=ev:s=find:e=input:t=q ; {"value": "ad", "rev": 2} ST
host → ESC ] 7279 ; a=ev:s=find:e=change:t=q ; {"value": "ad", "rev": 2} ST
host → ESC ] 7279 ; a=ev:s=find:e=submit:t=search ; {"fields": [["q", "ad"]]} ST
```

- **`submit` uses name-value pairs** because multiple controls can share a name
  (such as checkboxes in a group). A form with no named controls sends `[]`.
  Forms never make network requests; `submit` is the entire action.
- **Text field commits** occur when the user presses `enter` in an `<input>`
  (whether inside a `<form>` or not; new in 0.6) or when the field loses focus,
  including when the surface loses the keyboard. `change` is sent only if the
  value differs from the last committed value. Without `data-on="input"`,
  `change` is the only event emitted for the field.
- **`rev` is a user-edit counter** that increments on every user edit, toggle,
  check, or selection. Caret movement and programmatic updates do not increment
  `rev`. Pass `rev` back in `a=field` to update a control safely (see below).
- **Programmatic updates** via a delta to `value`, `checked`, or `selected`
  override the control's state, except while the user is actively editing that
  control ([Updates](https://hotty.neuroplast.io/docs/concepts/updates)).

## Changing what the user is changing

> [!NOTE]
> New in 0.5.

To modify a control that the user may be editing—for example, to apply
autocomplete in place—send `a=field` ([§6.4](https://hotty.neuroplast.io/docs/protocol#64-afield)) with the
last `rev` your program received. The host applies the update only if the
control's current `rev` still matches, preserving the field's `id`, undo
history, and keybindings, and placing the caret after the inserted text.

```text
host    → ESC ] 7279 ; a=ev:s=find:e=input:t=q ; {"value": "sr", "rev": 2} ST
program → ESC ] 7279 ; a=field:s=find:t=q ; {"rev": 2, "from": 0, "to": 2, "text": "src/"} ST
```

- **Payload fields:** `rev` plus one of:
  - `value` to replace the entire value;
  - `text` to replace the current selection or the byte range `from`..`to`;
  - `checked` for checkboxes and radio buttons.

  Include `sel` (`[anchor, caret]`) to set the selection after the edit, or send
  `sel` alone to move the selection without changing the text.
- **Byte offsets:** positions are UTF-8 byte offsets (for example, `é` is 2
  bytes). JavaScript programs must convert from UTF-16 code units.
- **Conflict handling:** if the user edited the control before `a=field`
  arrived, the host leaves the control unchanged and returns `ESTALE` with the
  control's current `rev`. Wait for the next event and recompute the update
  rather than retrying blindly, which would overwrite user input.
- **No echo events:** `a=field` does not emit `input` or `change`. If a field
  loses focus after being modified only by the program, no `change` is emitted.
- **Undo:** the host records an `a=field` edit as a single undo step, just like
  a user edit.
- **Caret position:** a field's caret starts at the end of its value and remains
  where the user left it while another element is focused.

## Presses

A placement with `p=1` reports every pointer press inside its bounds, regardless
of whether the user pressed a control, plain text, or empty space. Use this to
track which surface is active—for example, selecting a card in a dashboard.

```text
program → ESC ] 7279 ; a=place:s=cpu:c=40:r=6:p=1 ST
host    → ESC ] 7279 ; a=ev:s=cpu:e=press:t=cpu-card ; {"area": {"c": 0, "r": 0, "w": 40, "h": 6}} ST
```

- **`t`** is the nearest ancestor (or target) element with an `id`. If no
  ancestor has an `id`, both `t` and `area` are omitted.
- **Ordering:** `press` is always emitted before any other event triggered by
  the same interaction (`change`, `focus`, and `click` on release).
- **Triggers:** `press` fires for a primary mouse button press, a tap, or a
  touch that transitions into a drag. It does not fire when the pointer passes
  through the surface, for mouse or pen presses with Alt held, or when the host
  consumes the press for a hyperlink gesture.

## Drags

Add `data-on="drag"` to an element to track pointer movement from press to
release ([§9.1](https://hotty.neuroplast.io/docs/protocol#91-drags)). Drags support interactions that clicks
cannot, such as range selection across a grid, drag-and-drop reordering, and
sliders.

- **`dragstart`** fires on press. `t` is the element with `data-on="drag"`,
  which must have an `id` or no drag starts.
- **`drag`** fires whenever the element under the pointer changes. `t` is the
  nearest element under the pointer that has an `id` and `drag` or `drop` in
  `data-on`. If no such element is under the pointer, `drag` fires on each cell
  boundary crossed instead.
- **`dragend`** fires once on release, regardless of where the pointer is.

Every drag event includes `c` and `r`, the pointer's cell coordinates relative
to the surface's top-left corner. A touch's drag also carries `finger`, so two
fingers can drag at once ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#a-drag-per-finger)). Coordinates continue outside the surface and
can be negative above or to the left. Because `drag` fires on target or step
changes rather than every pixel moved, event traffic stays low.

**Sub-element steps:** add `data-steps="20"` to divide an element's width into
20 discrete steps, adding `x` (`0`..`20`) to every drag event. Use
`data-steps="0 20"` to divide height instead, reporting `y`. A `drag` event is
also emitted whenever the step changes, as in this volume slider:

```html
<div id=volume data-on=drag data-steps=20 style="touch-action: pan-y">…</div>
```

```text
host → ESC ] 7279 ; a=ev:s=mixer:e=dragstart:t=volume ; {"c": 4, "r": 1, "x": 5} ST
host → ESC ] 7279 ; a=ev:s=mixer:e=drag:t=volume ; {"c": 5, "r": 1, "x": 6} ST
  …
host → ESC ] 7279 ; a=ev:s=mixer:e=dragend:t=volume ; {"c": 7, "r": 1, "x": 9} ST
```

Steps are measured against the dragged element's bounds and clamped, even if the
pointer leaves the element. This lets a slider continue tracking when a finger
or cursor drifts off the track.

**Drop targets:** mark an element with `data-on="drop"` and an `id` so drags can
target it without making the element itself draggable. If a drop target also
defines `data-steps`, drag events over it include `tx` and `ty` for the step
within that target.

A list row with `data-on="drag drop"` and `data-steps="0 1"` acts as both a
draggable item and a drop target split into top and bottom halves (drop before
or after). A tree folder with `data-steps="0 2"` splits into three zones:
before, inside, and after.

- **Pointer capture:** the host captures the pointer until release (like
  `setPointerCapture` in the DOM), delivering all motion to the drag even over
  terminal cells, other surfaces, or outside the terminal window.
- **No text selection:** dragging on an element with `data-on="drag"` does not
  select text.
- **Click fallback:** if the pointer is released on the same element where the
  drag started, and that element also has `click` in `data-on`, the host emits
  `click` after `dragend`.
- **Cancellation:** if the placement is removed, the document is replaced, the
  host loses pointer capture, or a gesture takes the finger over, the drag ends
  early with a `dragend` event that omits `t` and carries `"cut": true`. Deltas
  and re-placements do not cancel an active drag.

## What a cell holds

> [!NOTE]
> New in 0.8.

When your program handles a drag outside hotty—such as a terminal multiplexer
dragging a pane using standard terminal mouse reporting—the surface receives no
pointer events. To hit-test which draggable or drop-target element sits at a
given surface cell, send `a=hit`:

```
ESC ] 7279 ; a=hit:s=nav:c=3:r=7:n=4 ST
```

The host replies with `a=ok` and `{"t": str, "tx": int, "ty": int}`: the nearest
element at `(c, r)` with an `id` and `drag` or `drop` in `data-on`, along with
its step coordinates if configured
([§9.7](https://hotty.neuroplast.io/docs/protocol#97-what-a-cell-holds-ahit)). `a=hit` has no side effects
(no focus change, hover update, or events); your program renders any drop
indicator itself via a delta.

## Touch on a surface

A tap on a surface acts as a click, and a touch drag scrolls like a mouse wheel
([§9](https://hotty.neuroplast.io/docs/protocol#9-events)). Over a scrollable document, touch drags scroll
the document until it hits its scroll limit. Over a non-scrollable surface,
touch drags fall through to the underlying terminal cells (terminal scrollback,
or wheel input on the alternate screen).

To prevent surfaces from trapping touch scrolling, touch starts a drag only when
the element explicitly disables panning along the drag axis:

- **`touch-action`** controls touch behavior via standard CSS. On an element
  with `data-on="drag"`, a touch begins a drag only if `touch-action` disallows
  panning in the initial direction of movement.
- **`pan-y`** on a horizontal slider lets horizontal swipes drag the slider
  while vertical swipes still scroll. `none` enables dragging in any direction.
  The default (`auto`) always scrolls. A first move as far across as down
  scrolls where a pan is allowed either way, and drags under `none`.
- **Event order:** when a touch becomes a drag, the host emits `press` (if
  enabled) followed by `dragstart`, both at the touch start coordinates.
- **Taps and long presses:** a tap emits `press` on lift. A long press never
  becomes a drag.
- **Multi-touch:** each finger drags, taps, or long-presses on its own, while
  the others drag. Two fingers on an element with `gesture` in `data-on` are a
  pinch the program hears; elsewhere, once a finger pans, the host owns the
  rest until all fingers lift
  ([Pointers and touch](https://hotty.neuroplast.io/docs/concepts/pointers#fingers-and-drags)).

Touch with Alt held never starts a drag, and touch movement does not trigger
`hover`. A host may optionally report hover on the target touched at the end of
a tap or touch drag.

## Presses with Alt

A mouse or pen press with Alt held always bypasses surfaces and goes to the
underlying terminal cells ([§9.2](https://hotty.neuroplast.io/docs/protocol#92-presses-with-alt)). If the
program has terminal mouse reporting enabled, it receives the press with the Alt
modifier set (for example, `CSI < 8 ; <col> ; <row> M` in SGR encoding).

This lets programs implement global gestures over their own surfaces—for
example, a multiplexer moving panes with Alt+drag like a window manager.

- **Surfaces see nothing:** no `press`, `dragstart`, or `click` is emitted, no
  focus is taken, and no hyperlink is opened. If a surface currently holds the
  keyboard, it releases focus and emits `blur`.
- **Sticky for the gesture:** the modifier state at press time determines
  routing until release. Releasing Alt mid-drag does not transfer the gesture to
  a surface, and pressing Alt during an active surface drag does not cancel it.
- **macOS:** Alt corresponds to Option, regardless of the terminal's Option-as-Meta
  or character-composition settings.
- **Mouse and pen only:** touch taps always behave as clicks regardless of held
  modifier keys.

## Where the pointer passes through

On hosts advertising the `passthrough` feature, a surface only captures pointer
events where an interactive CSS box is hit
([§9.3](https://hotty.neuroplast.io/docs/protocol#93-where-the-pointer-passes-through)). The host performs
standard browser hit-testing and ignores boxes with `pointer-events: none`. This
allows an overlay frame around terminal cells to leave the interior cells
clickable:

```html
<style>
  html { pointer-events: none }
  #close { pointer-events: auto }
</style>
<div class=frame><button id=close>Close</button></div>
```

Where hit-testing misses, the pointer falls through to whatever lies beneath—another
surface or the terminal cells. Over terminal cells, your program receives normal
mouse reporting as if no surface were present, the surface emits no events, and
a press returns keyboard focus to the terminal.

Pointer ownership is fixed at press time until release. On hosts without
`passthrough`, the entire surface rectangle captures all pointer events. If your
program needs pointer access to cells beneath a surface, check for the
`passthrough` capability and avoid covering those cells when it is absent.

## Hover

CSS `:hover` states and cursor shapes work locally without involving your
program. If your program also needs to know which element is under the pointer
(for example, to display a custom tooltip or clear a highlight), enable `v=1` on
the placement ([§9.4](https://hotty.neuroplast.io/docs/protocol#94-hover)):

```text
program → ESC ] 7279 ; a=place:s=chart:c=60:r=12:v=1 ST
host    → ESC ] 7279 ; a=ev:s=chart:e=hover:t=bar-7 ; {"c": 31, "r": 4} ST
host    → ESC ] 7279 ; a=ev:s=chart:e=hover ; {"out": true} ST
```

- **Target changes only:** `hover` fires when the nearest element with an `id`
  under the pointer changes, reporting `t` and the pointer cell `(c, r)`. Moving
  within the same element emits nothing, and events are throttled to at most one
  per frame.
- **Pointer leave:** `{"out": true}` fires when the pointer leaves the surface
  for terminal cells, another surface, a `pointer-events: none` region, or
  outside the terminal window. An Alt+press also emits `out`.
- **Suppressed during presses:** while a pointer button is held, no `hover`
  events are emitted (drags report their own targets). On release, the host
  reports the current hover target.
- **Mouse and pen only:** touch motion does not emit `hover`, though a host may
  report the element touched when a tap or touch drag lifts.

Hover reporting is gated by the `hover` capability. If a host lacks `hover`,
your program will not receive `out` events and should clear highlights on the
next key or press instead.

## What is in view

Scrollable documents can report which marked elements are currently visible in
the viewport ([§9.5](https://hotty.neuroplast.io/docs/protocol#95-view)). Add `data-on="view"` and an `id`
to each element you want to track—useful for highlighting active sections in a
table of contents or lazy-loading items in a long list.

```text
host → ESC ] 7279 ; a=ev:s=guide:e=view ; {"in": {"install": {"c": 0, "r": -3, "w": 60, "h": 8}, "usage": {"c": 0, "r": 5, "w": 60, "h": 20}}, "end": false} ST
```

- **`in`** maps every currently visible tracked element's `id` to its full
  bounding box. Because each event is a complete snapshot rather than a diff,
  your program does not need to track incremental visibility state.
- **`end`** is `true` when the document root is scrolled to the bottom.
- **Timing:** `view` fires once after initial layout, and again whenever the
  payload changes. During active scrolling, the host waits for scrolling to
  settle before emitting.

To scroll an element into view programmatically, use the `reveal` delta op. To
scroll in key-sized increments from your own keymap, use the `scroll` op
([Updates](https://hotty.neuroplast.io/docs/concepts/updates)).

## Touch in the cells

> [!NOTE]
> New in 0.4.

By default, touch dragging over terminal cells scrolls the terminal rather than
generating mouse drag events, so cell-rendered sliders or splitters work with a
mouse but not with touch. Use `a=touch` to declare cell rectangles where touch
drags along a specified axis should be translated into mouse drags
([§9.6](https://hotty.neuroplast.io/docs/protocol#96-touch-in-the-cells)):

```text
program → ESC ] 7279 ; a=touch ; {"areas": [{"c": 10, "r": 20, "w": 30, "h": 1, "drag": "x"}]} ST
```

Each area specifies:

- `c` and `r`: top-left column and row, zero-indexed from the screen origin
  (note that terminal mouse reports are 1-indexed);
- `w` and `h`: width in columns and height in rows;
- `drag`: allowed drag axis—`"x"` (horizontal), `"y"` (vertical), or `"xy"`
  (either).

Inside a declared area, a touch moving along the configured axis is synthesized
as a primary mouse button press at the start cell, drag motion as the finger
moves, and release on lift. Your program receives standard terminal mouse events
in whatever mode and encoding it enabled, with no extra event format to parse.

- **Taps and orthogonal swipes:** a tap inside an area synthesizes a press and
  release on lift (so a slider jumps to the tapped position). Swiping along the
  non-drag axis still scrolls.
- **Active conditions:** translation applies only while the program has mouse
  button reporting enabled and the terminal is not viewing scrollback.
- **Lifetime:** areas persist until replaced by another `a=touch` or cleared
  with an empty `a=touch`. The main and alternate screens maintain separate area
  lists.
- **Screen-relative:** coordinates are tied to screen cells, not buffer lines,
  so they do not move when terminal text scrolls. Resend areas when your UI
  moves, and clear them on exit.
- **Surface precedence:** if a touch starts on a surface that accepts pointer
  input, the surface handles it.
- **Touch only:** mouse and pen input are unaffected.

Hosts without touch support still accept `a=touch`, so you can send areas
unconditionally. Multiplexers that forward mouse input should forward each
pane's touch areas translated to the pane's screen offset. SDKs construct this
command with `Touch` ([SDK design](https://hotty.neuroplast.io/docs/sdk-design#34-commands)).
