Skip to content

core: Deliver typed characters to typeahead - #4023

Merged
huacnlee merged 5 commits into
mainfrom
agent/T-12660-typed-text
Dec 2, 2026
Merged

huacnlee merged 5 commits into
mainfrom
agent/T-12660-typed-text

Conversation

@huacnlee

@huacnlee huacnlee commented Dec 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Typeahead in Select, Combobox, Menu, Tree and Command bound 36 fixed keys (a–z, 0–9). Shift capitals, non-US layouts and accented letters never reached it, and no extra bindings can fix that: a keystroke names a US key position, not the text it types. This PR adds a small core channel for typed characters, as the decision on #3991 asked:

  • Core (nsh-core): on_key_char(KeyOptions, |text| ..) runs a handler with the text a key types on the layout in use, in its case (Shift+A is "A", a French key "é", a Russian key "ж"). It fires only for a key that no keystroke binding took. It does not fire while a text input edits or an input method composes, for Space alone, for named keys or control characters, or with Command, or with Control/Alt unless AltGr typed the key. Option counts on Apple platforms, where it types characters. The key is consumed. Bindings live in the root keymap's own list. The platform matches them with Keymap::find_typed after the keystroke bindings, and reports them through the existing root KeyCommand event with a { id, text } payload.
  • nsh-base: bind_typeahead (with listbox: Take a capital and a Space in typeahead #4010's structure: reset when the active item changes, Space inside a string) now listens to on_key_char instead of binding TYPEAHEAD_KEYS. While a typeahead string is in progress (a character within the 1 s typeahead pause), Space adds a space instead of choosing or opening: ice c reaches "Ice cream".
  • Backends: each one calls find_typed where it already matches the keymap:
    • macOS: NSEvent.characters
    • Windows: the queued WM_CHAR, AltGr as Ctrl+Alt, no VK_PROCESSKEY
    • Linux/GTK: the keyval's Unicode
    • GPUI: Keystroke::key_char
    • Web: DOM key, not while isComposing
    • iOS: UIKey.characters
    • Android: the key event's Unicode char
    • TUI: KeyCode::Char
    • Headless simulator: the US character, plus press_char(text) for keys no keystroke names

Public API

  • New: nsh_core::element::on_key_char (also in the prelude). Rust only; TSX has no equivalent in this PR.
  • Platform plumbing, #[doc(hidden)]: Keymap::with_typed, Keymap::takes_typed, Keymap::find_typed, keymap::typed_payload.
  • Test API: Headless::press_char.
  • Not overlapping keymap: Add named actions, key contexts and the JSON keymap #3957: typed text is not a keystroke-to-action mapping and is not rebindable.

Compatibility: JS never sees the new payload, because typed-character bindings are views' bindings with ids ≥ 2^31 that the core handles itself. Existing JS onKeyCommand payloads are unchanged. Nothing is registered in nsh-compat, since no release is recorded yet. on_key_char is new Rust surface to name-check before the 0.9.0 freeze.

Tests

  • nsh-core keymap: typed text goes to the newest binding of its window, never while editing, and is filtered by modifiers (Shift, Cmd, Ctrl, Alt, AltGr, Option on Apple).
  • nsh-base select: typeahead_takes_the_characters_a_key_types (Shift+I, é, Б), a_shortcut_with_command_or_control_is_not_typed; the space-in-a-string tests from listbox: Take a capital and a Space in typeahead #4010 (typeahead_takes_a_capital_and_a_space_while_typing, typeahead_on_the_closed_trigger_takes_a_space_while_typing) now run through on_key_char.
  • Backend helper unit tests where the text can be computed without a window (Windows WM_CHAR filtering, GTK keyval text, GPUI key_char, web key/composition, TUI).

Verification gap: no real-window check yet on Linux, Windows (embedded) or macOS; that follows on this PR.

Fixes #3991

🤖 Noop (rust-maintainer) · Opus 5.5 acting for @huacnlee

huacnlee and others added 3 commits December 2, 2026 18:03
Add on_key_char: the text a key types on the layout in use, in its
case, for a binding taking typed characters while no text input edits
or input method composes. Every backend matches it after the keystroke
bindings. Select, Combobox, Menu, Tree and Command typeahead listen to it
instead of 36 fixed keys, and Space adds to a typeahead string in
progress.

Fixes #3991

Agent: rust-maintainer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Agent: rust-maintainer
Resolve bind_typeahead against #4010: keep main's string reset and
Space-while-typing binding, fed by on_key_char instead of the fixed
letter and digit keys. Drop this branch's two Space tests, which #4010's
typeahead_*_space_while_typing tests now cover.

Agent: rust-maintainer
@huacnlee

huacnlee commented Dec 2, 2026

Copy link
Copy Markdown
Member Author

Merged main into this branch (d40da9c) to clear the conflict with #4010.

  • crates/nsh-base/src/listbox.rs: kept listbox: Take a capital and a Space in typeahead #4010's bind_typeahead (string reset when active changes, Space joining the string over the focused control's Space), now fed by on_key_char instead of the fixed letter/digit keys, so capitals, é and Cyrillic still reach typeahead.
  • crates/nsh-base/tests/base/select.rs: kept both sides' fixtures and tests, except this branch's two Space tests, which listbox: Take a capital and a Space in typeahead #4010's typeahead_takes_a_capital_and_a_space_while_typing / typeahead_on_the_closed_trigger_takes_a_space_while_typing now cover with main's behaviour.

cargo test -p nsh-base passes (1249 + 1301 + 1), cargo clippy -p nsh-base --tests -D warnings is clean (macOS).

🤖 Noop (rust-maintainer) · Opus 5.5 acting for @huacnlee

KeyFocus is non_exhaustive, so the Windows and Linux keymaps can't use
a struct expression for it. The gpui typed-key test counted events sent
to an engine it never starts; check what each key returns instead.

Agent: rust-maintainer
@huacnlee
huacnlee merged commit a534ec3 into main Dec 2, 2026
15 of 38 checks passed
@huacnlee
huacnlee deleted the agent/T-12660-typed-text branch December 2, 2026 15:14
madcodelife added a commit to longbridge/nsh-compat that referenced this pull request Dec 2, 2026
## What

The simulated keyboard of the apps check (`Views::press_key`) now
delivers the character a key types to a binding of typed characters.
When no keystroke binding takes a `Key::Char`, it types its character on
the US layout (Shift's character with Shift) and matches it with
`Keymap::find_typed`. When a binding takes it, `typed_payload(id, text)`
is reported to the root as a `KeyCommand` and the key is consumed. This
mirrors nsh's headless driver (`Headless::send_typed`) from
longbridge/nsh#4023.

## Why

longbridge/nsh#4023 moved typeahead (`Select`, `Combobox`, `Menu`,
`Tree`, `Command`) from keystroke bindings of `a`–`z` / `0`–`9` to typed
characters (`on_key_char`). Since then, a letter pressed here reaches no
binding, and the Compat check on nsh `main` fails:

```
case-browser: step 59 `expect Log: chose DeskItem`: not shown
```

In that case, `key d` on a menu opened by a press should move the focus
to `DeskItem`, and `key enter` should choose it.

## Test plan

Against nsh `main` (ea517c992) with this change:

- `NSH_COMPAT_APP=browser cargo xtask compat -v`: passes (step 59
included).
- `cargo xtask compat -v`: 39 passed, 0 failed.
- `cargo fmt --check` and `cargo clippy --all-targets -- -D warnings`:
clean.

Without this change, the browser case fails at step 59 on nsh `main` and
at longbridge/nsh@a534ec3a1. It passes at that commit's parent
(5f62f41bd).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

listbox: Type ahead with the characters the keyboard types

1 participant