---
title: "Input"
description: "Terminal apps, written in TypeScript and compiled to native executables with scriptc."
canonical_url: "https://tui-sdk.dev/docs/concepts/input"
---
# Input

`runApp` sends a key to the focused part first, then to the app's `keys` callback. The callback returns `true` when it handled the key. `bindTerminal` gives every key to its `onKey` callback instead.

The HTML page runs [`examples/keys.ts`](https://github.com/vercel-labs/tui/blob/main/examples/keys.ts) here, in a terminal in the page.

## The Key value

A `Key` says what happened:

- `name` is the key: `"char"`, `"enter"`, `"up"`, `"backspace"`, `"f1"`, `"paste"`, `"mouse"`, and so on.
- `text` is the character typed, the pasted text, or the terminal's reply. `base` is the key itself, so a and A are both `"a"`.
- `ctrl`, `alt`, and `shift` say which modifiers were held.
- `event` is `"press"`, `"repeat"`, or `"release"` when the terminal sends key events.
- `alone` is true when the key was the only one in its read from the terminal, so a `y` inside a paste never confirms anything.
- `mouse` carries the action, button, and cell for a mouse key.

`matchesShortcut` compares a key with a shortcut such as `"ctrl+k"`, `"alt+enter"`, or `"shift+tab"`:

```ts
import { createKeyReader, matchesShortcut } from "tui"

const reader = createKeyReader()
const keys = reader.read("\x1b[A")

for (const key of keys) {
  console.log(key.name, matchesShortcut(key, "up"))
}
```

`createKeyReader` turns terminal bytes into keys across reads. A paste is one `paste` key with all of its text. The key reader is what `bindTerminal` uses.

## Text input

`Input` edits like a shell: the arrow keys move, Home and End go to the ends, and a paste arrives whole. It is controlled or uncontrolled through `value`, `defaultValue`, and `onValueChange(value, details)`. A test app can type into it and read the screen back:

```ts
import assert from "node:assert/strict"
import { createTestApp, Input } from "tui"

const state = { value: "" }

const app = createTestApp({
  columns: 20,
  rows: 1,
  view: (frame) => {
    Input.Root(
      frame,
      {
        id: "name",
        label: "Name",
        value: state.value,
        placeholder: "Name",
        onValueChange: (value) => {
          state.value = value
        },
      },
      (root) => Input.Field(root, frame.rect),
    )
  },
})

app.type("Ada")
assert.equal(state.value, "Ada")
assert.deepEqual(app.screen(), ["Ada"])
console.log(app.screen().join("\n"))
```

## Focus

The first focusable part drawn takes the focus. Parts register in draw order, so draw the part you want focused first. `ScrollArea.Viewport` is focusable: in an app with an input and a scroll area, draw `Input.Field` or `Textarea.Field` before `ScrollArea.Root`, or the scroll area takes the focus and typing never reaches the input. Tab and Shift+Tab then move the focus between parts that take keys. An open `Dialog` keeps the focus and the keys inside it until it closes. [Rendering model](/docs/concepts/rendering-model) shows where the cursor goes: a focused `Input` sets `frame.cursor`.

## Mouse

An app with `mouse: true` takes the mouse on the alternate screen. A click focuses the part under it: it picks a `Listbox` item, checks a `CheckboxGroup` item, opens a tab, picks a `Table` row, and puts an `Input` or `Textarea` cursor where it lands. The wheel moves the part under it as the arrow keys do.

Anything no part takes, such as a release or a click on a canvas, goes to `keys` as a `mouse` key:

```ts
import { createTestApp, type Frame, text } from "tui"

let clicked: { x: number; y: number } | null = null

const app = createTestApp({
  columns: 20,
  rows: 2,
  mouse: true,
  view: (frame: Frame) => {
    text(frame, 0, 0, clicked ? `Clicked ${clicked.x},${clicked.y}` : "Click a cell")
  },
  keys: (key) => {
    if (key.mouse?.action === "press") {
      clicked = { x: key.mouse.x, y: key.mouse.y }
      return true
    }
    return false
  },
})

app.click(5, 1)
console.log(app.screen().join("\n"))
```

While an app has the mouse, most terminals select text only with Shift held.

## Held keys

An app with `heldKeys` can ask `frame.held("w")`, `frame.held("space")`, or `frame.held("shift")`. Terminals with the kitty keyboard protocol, such as kitty, Ghostty, WezTerm, foot, and recent iTerm2 and Alacritty, report a key's release. Other terminals never do, so there a key counts as held until a moment after its last press or repeat.
