Skip to content

Input, key bindings, focus and mouse

The Program decodes terminal bytes with an input.Reader and passes each event to Update as a message: tui.Key, tui.MouseEvent, tui.PasteEvent, tui.FocusEvent, and the others listed below. These root types are aliases of the input types, so tui.Key and input.Key are the same type. The KeyType, MouseButton and MouseAction constants are re-exported too (tui.KeyEnter, tui.MouseButtonLeft). The modifier bits are not: use input.ModCtrl, input.ModShift and so on.

func (c counter) Update(msg tui.Msg) (tui.Model, tui.Cmd) {
if k, ok := msg.(tui.Key); ok && k.Type == tui.KeyRunes {
switch k.Text {
case "+":
c.n++
case "q":
return c, tui.Quit()
}
}
return c, nil
}

From the counter model of ExampleNewProgram in example_test.go.

Program options (WithMouse, WithKeyboard, …) are covered in Program. This page covers handling the events.

A Key has a Type. Printable input is KeyRunes, with the typed text in Key.Text. Ctrl+C is KeyCtrlC. Other Ctrl+letter keys are KeyCtrl, with the letter in Key.Code. Modifiers on arrows, Home/End, PgUp/PgDn and Alt+rune arrive in Key.Mod. Key.String() gives the name used everywhere else in the library ("a", "up", "ctrl+right", "enter"):

rd := input.NewReader(strings.NewReader("a\x1b[A\x1b[1;5C"))
for {
ev, err := rd.ReadEvent()
if err != nil {
break
}
fmt.Println(ev.(input.Key))
}
// Output:
// a
// up
// ctrl+right

From ExampleReader in input/example_test.go. The Reader is a plain decoder over an io.Reader, so it needs no TTY.

A lone ESC waits input.DefaultEscTimeout (30ms) for the rest of a sequence before it becomes KeyEsc. Change this with tui.WithEscTimeout.

The legacy encoding cannot tell Shift+Enter from Enter. tui.WithKittyKeyboard(true) turns on the kitty keyboard protocol’s “disambiguate” flag, and terminals without the protocol ignore it. tui.WithKeyboard(tui.KeyboardReportEvents) also reports repeats and releases. They arrive as tui.KeyRepeatMsg and tui.KeyReleaseMsg, never as Key, so case tui.Key still sees only presses.

Bracketed paste is on by default. A paste arrives as one tui.PasteEvent with the whole text, not as keys, so a pasted newline is not an Enter press. One event holds at most input.MaxPasteBytes (16 MiB). Truncated is set if the paste was longer. If the end marker does not arrive within input.PasteIdleTimeout (250ms) of the last byte, the event carries what was received and Incomplete is set. tui.WithBracketedPaste(false) turns this off.

tui.WithFocusReporting(true) sends a tui.FocusEvent{Focused: bool} when the terminal window gains or loses focus. Only changes are delivered. This is the window’s focus, not focus between widgets, which is covered below.

keymap keeps a widget’s keys and its help text in one place. A keymap.Binding is a description plus key names (the Key.String() forms). keymap.Matches(msg, b) reports whether msg is a press of one of them. Releases and disabled bindings never match. Repeats count as presses.

Widgets that react to keys follow one convention. The package has a KeyMap struct with one Binding per action, DefaultKeyMap(), a KeyMap field on Model, and a Bindings() method that returns the bindings in effect. To rebind a widget, change its field, for example m.KeyMap.Down. The behaviour and the help text change together. The packages that follow it are marked in the widget catalog.

A keymap.Registry collects bindings for help screens and reports two actions bound to the same key in one scope:

var r keymap.Registry
r.Add(keymap.NewBinding("quit", "q"))
conflicts, _ := r.Add(keymap.NewBinding("close", "q"))
fmt.Println(len(conflicts))
for _, h := range r.Hints("") {
fmt.Println(h.Key, h.Desc)
}
// Output:
// 1
// q quit
// q close

From Example in keymap/example_test.go.

To build help from what is actually bound, implement tui.BindingsProvider on your root model by returning the focused widget’s Bindings() and your own. (*tui.Program).Keymap() then returns a fresh Registry built from it, as of the last frame drawn. Pass that registry to helpscreen.FromRegistry or widgets.HintsFromKeymap. Build the help screen when it opens, so it follows the focus.

tui.WithChords(defs...) recognises multi-key sequences such as g g or ctrl+x ctrl+s. Each is a tui.ChordDef{Name, Keys}, with Keys in Key.String() form. When the keys complete a chord, Update receives one tui.ChordMsg{Name} instead of the keys. A key that cannot start or continue a chord is delivered at once. A prefix that turns out not to be a chord is delivered as its original keys, in order. So is a prefix still waiting when the timeout passes, even if no other key is typed. A key that can start a chord is therefore held back for up to the timeout: input.DefaultChordTimeout (500ms), changed with tui.WithChordTimeout. chords_test.go covers each case.

Outside a Program, input.NewChordMatcher does the same matching. You call Feed for each Key and Expire from a timer. The repository has no Example for chords.

A focus.Ring tracks which of n items has focus. Tab and Shift+Tab move it, wrapping at the ends and skipping disabled items. Like the widget Models it is a value: every method returns a new Ring. Ring.Route does the whole dispatch. On Tab and Shift+Tab it blurs the old item and focuses the new one. It sends every other message to the focused item only.

func (m *model) fields() []focus.Field {
return []focus.Field{
focus.Bind(&m.name),
focus.Bind(&m.password.Model), // passwordinput embeds a textinput.Model
focus.Bind(&m.bio),
}
}
// Route does the whole dispatch: Tab and Shift+Tab (the input reader
// decodes both ESC [ Z and the kitty form to a Tab key with the Shift
// modifier) blur the old widget and focus the next one, wrapping in both
// directions; every other message goes to the focused widget only.
var cmd tui.Cmd
m.ring, cmd = m.ring.Route(msg, m.fields()...)
return m, cmd

From examples/hello/main.go, a small program that moves focus between a field and two buttons.

  • focus.Bind(&w) works for any widget whose pointer has Focus() tui.Cmd and Blur() (focus.Focusable). Bind fields of the model value you are about to return. The pointers must point into that copy.
  • Call ring.Sync(fields...) once at start-up and return its Cmd from Init. Call it again after Set or SetDisabled.
  • Route does not broadcast. Forward messages every widget needs, such as blink ticks for an unfocused field, yourself.
  • ring.Push(n) opens a scope that traps Tab inside a modal. Pop restores the previous scope and its focused item.
  • ring.WithOrder(focus.LayoutOrder(root, size, names...)) makes Tab follow screen position rather than index order (ExampleLayoutOrder in focus/order_test.go).
  • ring.RouteAuto(msg, focus.Zones(root, size, names...), fields...) also focuses the item under a left click. It also lets an item keep Tab for itself through Field.Consumes. examples/settings uses it.

Mouse reporting is off by default. Turn it on with tui.WithMouse(tui.MouseClick) (press and release), tui.MouseCellMotion (also drags) or tui.MouseAllMotion (all movement). tui.EnableMouse(mode) returns a Cmd that switches it at run time. A tui.MouseEvent has 0-indexed cell coordinates X, Y, a Button (the wheel is four buttons), an Action and a Mod.

hittest answers which region a cell is in. A hittest.Map[ID] is an ordered set of rectangles. Later additions win where they overlap. Take the rectangles from the layout rather than computing them:

ui := layout.Row(1,
layout.FlexChild{Node: layout.Named("list", layout.Block("one\ntwo\nthree")), Basis: 8},
layout.FlexChild{Node: layout.Named("detail", layout.Block("details")), Grow: 1},
)
size := layout.Size{W: 30, H: 5}
var m hittest.Map[string]
for _, p := range layout.Rects(ui, size) {
if p.Name != "" {
m = m.Add(p.Name, p.Rect)
}
}
for _, click := range [][2]int{{2, 1}, {20, 4}} {
if h, ok := m.At(click[0], click[1]); ok {
fmt.Printf("click %v -> %s at %d,%d\n", click, h.ID, h.LX, h.LY)
}
}
// Output:
// click [2 1] -> list at 2,1
// click [20 4] -> detail at 11,4

From Example_fromLayout in hittest/layout_test.go.

hittest.HitMap(root, size) builds the same map in one call, keyed by name. Map.AtEvent(ev) tests a MouseEvent directly, and you decide which actions count. layout.Rects coordinates are relative to the root’s top-left. If the root is not drawn at the screen origin, add its offset. In inline mode (WithAltScreen(false)) mouse coordinates are absolute in the terminal, so offset your rectangles by where the live region starts.

textarea.Model and textinput.Model have a Vi field. Set it and the widget is modal: it starts in Normal mode, where keys are commands, and i a I A o O (o O in the text area only) enter Insert mode, where typing and the KeyMap work as usual until Esc.

ed := textarea.New()
ed.Vi = true
ed.Focus()

Supported: counts; the motions h l j k w W b B e E 0 ^ $ gg G f F t T ; ,; the operators d c y with a motion, a doubled key for lines (dd), or a text object (iw aw i" a" i( a( i[ i{); x X D C s S Y p P J r ~; u and Ctrl+R; . to repeat the last change; and v / V to select. A change, including the text typed in an Insert session, undoes as one step. y copies with OSC 52, as Copy does. Not supported: macros, named registers, marks, search, block-wise selection. The engine counts runes, so a grapheme cluster of several runes is several steps in the text area (the single-line input counts clusters).

Keys with Ctrl or Alt keep their KeyMap meaning in every mode, except Ctrl+R in Normal mode, which redoes. The single-line input leaves Up, Down and Enter to the field in every mode, so History still works. ViMode() and ViPending() read the state for a status line, and SetViMode(ViInsert) starts the widget in Insert mode:

status := map[textarea.ViMode]string{textarea.ViNormal: "NORMAL", textarea.ViInsert: "INSERT",
textarea.ViVisual: "VISUAL", textarea.ViVisualLine: "V-LINE"}[ed.ViMode()]

Set TypeAhead: true on a picker, menu, treeview, filepicker or datatable and typing letters jumps to the next item whose label starts with them, ignoring case: b finds the next one that starts with b, the same letter again goes on to the one after it, bl finds the first that starts with bl from where you began typing, and a pause of a second starts a new word. A virtuallist does it when it has a Label function, which it calls for items from the cursor on until one matches, so it works on a million rows.

A key that a binding uses keeps its meaning, so a letter bound to something (s and / in a datatable, a letter you added to KeyMap) cannot start a word. Filter on a picker or treeview wins over TypeAhead, since typing narrows the list there; a datatable with a Source does not search; in CellMode a datatable searches the column the cursor is in, and otherwise the first. dropdown has it built in.

A label can name its own hot key with an ampersand: "&Save" is the label “Save” with S underlined, and Alt+S presses it. "&&" is a literal ampersand, and a marker needs a letter or digit after it. button, buttongroup and tabs read it:

  • a button presses on its hot key whether or not it has focus (a disabled or loading one ignores it and is drawn without the underline), button.Model.Mnemonic returns the letter and IsMnemonic(msg) tests a message, Bindings lists alt+s, and Linearize says “shortcut Alt+S”;
  • a button group moves its cursor to the button whose hot key was pressed and presses it, even when the group has no focus;
  • tabs activate the tab whose hot key was pressed and deliver the usual ChangedMsg.

The marker takes no room: a labelled widget is as wide as an unlabelled one. menubar has its own Menu.Accel for the same Alt+letter. Two widgets with the same letter both act, so keep letters unique within a screen.

textarea has four opt-in aids that draw over the text, a Highlighter and a selection, and work with soft wrap and Height:

  • HighlightCursorLine gives the cursor’s line the background CursorLineColor (a dark gray), across the whole width when Width is set, and every row of it when it is wrapped;
  • MatchBrackets reverses and bolds the bracket under the cursor, or just before it, and its partner, for (), [] and {}. It does not know about strings or comments and gives up after 20,000 runes;
  • ShowWhitespace draws each space as WhitespaceMark (a middle dot) in a faint style, which also shows trailing spaces;
  • Ruler lists columns, counted from 1 in the cells as drawn, where every row gets the background RulerColor: Ruler: []int{80} is a line limit.

The cursor and the selection keep their own colours, and the aids use only colours and Faint, Reverse and Bold, so they follow the colour profile.

textinput and textarea have an opt-in kill ring, as readline and Emacs do. Set KillRing: true and Ctrl+K, Ctrl+U and Ctrl+W save what they delete (to the end of the line, to the start of it, the word before the cursor) in a ring of the last 16 kills. Kills in a row join into one entry, in document order. Ctrl+Y (KeyMap.Yank) inserts the newest, and Alt+Y (KeyMap.YankPop) right after replaces it with the next older one, round to the newest again. A yank is one undo step. A selection is deleted as before and saved nowhere; a field with DisableCopy (a password) never fills the ring.

Ctrl+Y was also the second key of Redo, so with the ring on it yanks while the ring has something in it and redoes only when it is empty. Redo keeps Ctrl+Shift+Z, and KeyMap.Redo and KeyMap.Yank can be set to whatever you like. With KillRing off, which is the default, nothing changes.

With the mouse on, the terminal no longer selects text for the user, and a widget only selects what it drew itself. WithTextSelection gives the user the terminal’s behaviour back for the whole screen:

p := tui.NewProgram(model,
tui.WithAltScreen(true),
tui.WithMouse(tui.MouseCellMotion),
tui.WithTextSelection(ansi.Style{}), // zero Style draws it in reverse video
)

Dragging with Alt held selects the cells under the pointer in reading order, whatever widget drew them; a double click with Alt takes a word and a triple click the row. A release copies the text with OSC 52 and delivers a tui.SelectionMsg{Text} to Update. The Program consumes the events that make the selection, so a widget never sees that drag; a plain click or drag reaches the widgets as usual and ends the selection, as does any key and a resize. The text is read from the last frame, so it is what the user saw, not what the model holds, and wrapped text is not joined back into one line.

clipboard.Model is a button that copies its Text to the system clipboard with an OSC 52 sequence on Enter or Space, then shows “Copied!” for Timeout (2 seconds from New). With Mouse set, a left click inside Bounds activates it too. The package is experimental.

m := clipboard.New("secret-token", "Copy")
var sent string
m.Write = func(s string) (int, error) { sent = s; return len(s), nil }
m, _ = m.Update(tui.Key{Type: tui.KeyEnter})
fmt.Println(m.Copied(), strings.HasPrefix(sent, "\x1b]52;"))
// Output:
// true true

From Example in clipboard/example_test.go. Write defaults to os.Stdout.WriteString. The example replaces it to capture the bytes.

textinput and textarea copy the selection with ctrl+c, cut it with ctrl+x and paste with ctrl+v (their KeyMap fields Copy, Cut and Paste). They do no I/O in Update: Copy and Cut return tui.WriteClipboard, and the Program writes the OSC 52 sequence to its own output and keeps the text. Paste returns tui.PasteCopied, and the Program delivers that text as a tui.PasteEvent, so route PasteEvent to the focused input as you do for a bracketed paste. The copied text belongs to the Program: copy in one input and paste in another works inside a Program, and two Programs in one process do not see each other’s copies. The paste key pastes what this Program copied, not the system clipboard; tui.ReadClipboard asks the terminal for that.

OSC 52 is write-only and best-effort, and the program cannot detect failure. The terminal must support it. Under tmux, set-clipboard must be on or external. If it is off, the button still shows “Copied!” but nothing reaches the clipboard.