Skip to content

Keyboard and pointer

Keyboard shortcuts and their recorders, polling keys, mouse buttons and pointer lock.

An editing role in the application menu takes the focused editor before its OnSelect fallback. On macOS, Undo and Redo follow that editor’s history, and Cut follows its selection, including while the app is inactive; with no editor, the fallback stays enabled.

On iOS, give cx.OnKeyWith a core.KeyOptions.Title function to offer the shortcut in the system’s keyboard menu while the binding can fire. Group names its menu on iPadOS 26 and later. The functions can read the app’s catalog so the shortcut titles follow the language. Use app.KeyCommands() in tests to inspect the current list.

cx.OnKey and cx.OnKeyWith also accept ordered multi-stroke shortcuts separated by spaces, such as "ctrl+k ctrl+c" or "g g". For dynamic keymaps built from slices of strokes, cx.OnKeySequence and cx.OnKeySequenceWith take []string and return an idempotent function that removes the binding before the owner disposes. KeyWhen and YieldsToMenu apply to the first stroke, and cx.KeyLabel formats each stroke separated by a space (⌃K ⌃C, Ctrl+K Ctrl+C). At each stroke the newest active binding wins: a complete binding runs once and clears any pending sequence, while a longer sequence consumes the stroke and waits up to core.KeySequenceTimeout (1.5 s) for the next stroke. Pressing Escape (unless expected by a pending sequence) or an unmatched key cancels and consumes the stroke without dispatching; pointer presses, focus changes, window focus loss, and hiding, covering with a modal, removing or disposing the owner clear pending state.

cx.OnKey("mod+k", func() { palette.Set(true) })
stop := cx.OnKeySequence([]string{"ctrl+k", "ctrl+c"}, commentLine)

A shortcut recorder lets the user choose such a key. A Focusable view’s OnKeyDown hears each key that goes down while it has the focus, before the app’s bindings, with the modifiers held, a modifier pressed alone included; returning true keeps the key from the bindings, so recording Command+S does not save. KeyEvent.Shortcut writes the press as OnKey and RegisterHotKey take it, each modifier by name (“ctrl+shift+k”, “alt+space”, “shift+meta+k” for Command+Shift+K), and “” for a modifier alone. Its OnKeyUp hears each of those keys come up, even after OnKeyDown consumed it or the focus moved, with the modifiers still held. Both work on macOS, Windows, Linux, the web, where the browser keeps the shortcuts it gives no page, Apple TV and in nsgotest; Android delivers hardware keyboard events to focused views asynchronously, so their OnKeyDown return value cannot consume the key. iPhone and iPad support polling hardware keys. A shortcut the app registered globally takes its key before the view, so a recorder unregisters it while it records.

core.View{
Focusable: func() bool { return true },
OnKeyDown: func(e core.KeyEvent) bool {
if s := e.Shortcut(); s != "" {
shortcut.Set(s)
}
return true
},
OnKeyUp: func(core.KeyEvent) { recording.Set(false) },
Children: []core.Node{core.Text{ContentFunc: shortcut.Get}},
}

A game or a canvas polls the keys instead. core.KeysDown(cx) lists the keys held on the window and core.KeyHeld(cx, "left") says whether one is: tracked reads, which a RequestAnimationFrame loop reads too. The first read has the platform report every key from then on, so a key already held stays unknown until it is pressed again; keys typed into a field or a web view, or while an input method composes, are never held, and a key still held as the window loses the focus comes up then. They work where OnKeyUp does and on iPhone and iPad with a hardware keyboard.

var loop func(time.Time)
loop = func(time.Time) {
if core.KeyHeld(cx, "left") {
x.Update(func(x float32) float32 { return x - 4 })
}
if core.KeyHeld(cx, "right") {
x.Update(func(x float32) float32 { return x + 4 })
}
cx.RequestAnimationFrame(loop)
}
cx.RequestAnimationFrame(loop)

For a control tied to a physical position, read KeyEvent.Code, core.KeyCodesDown(cx) or core.KeyCodeHeld(cx, "KeyW"). The code uses KeyboardEvent.code names: on AZERTY the QWERTY W position has Code: "KeyW" and Key: "z". core.KeyCodeLabel(cx, "KeyW") gives the active layout’s display label for a prompt. On iOS, Android, HarmonyOS and browsers without a layout map, it uses the last key name reported at that position and returns an empty string until a key is seen. HarmonyOS names both the position and the key from InputKit’s KeyCode, as ArkUI reports no scan code, so there the key name does not yet follow a non-QWERTY layout.

For mouse buttons, View.OnMouseDown and View.OnMouseUp deliver a core.MouseButtonEvent for every button (core.MouseButtonLeft, core.MouseButtonRight, core.MouseButtonMiddle, core.MouseButtonBack, core.MouseButtonForward) on macOS, Windows, Linux, the web and nsgotest, without making the view a keyboard focus stop. core.MouseButtonsDown(cx) and core.MouseButtonHeld(cx, core.MouseButtonRight) are tracked reads of the buttons held on the window, clearing automatically when the window loses focus.

In a game camera or a drag that needs to continue at the screen edge, call ref.LockPointer() from a press on the view that holds the ref. The pointer stays in place and is hidden while the view receives relative movement in OnPointerDelta(dx, dy). Positive values move right and down.

Call the returned function to release the lock. ref.PointerLocked() reports the current state and updates computations that read it. OnPointerUnlock runs when Escape, a window losing focus, a refused request, or hiding or removing the view releases the lock. It does not run when the app calls the returned function. Check cx.Env().CanLockPointer() to see whether the surface supports it. The web requires a user gesture such as a press. Linux on Wayland requires a compositor that supports pointer constraints and relative motion.