Skip to content

core: Add altGraph to key modifiers - #2979

Merged
huacnlee merged 2 commits into
mainfrom
agent/T-10919-altgraph
Nov 23, 2026
Merged

huacnlee merged 2 commits into
mainfrom
agent/T-10919-altgraph

Conversation

@huacnlee

Copy link
Copy Markdown
Member

Fixes #2936

On Windows, AltGr is sent as Left Control + Right Alt, so a key event typed with AltGr and a Ctrl+Alt shortcut both reported control and alt, with nothing to tell them apart.

API

  • Rust: keymap::Modifiers::alt_graph: bool
  • TS: Modifiers.altGraph: boolean (the DOM's getModifierState("AltGraph"))

Only key events set it. Shortcuts never name or match it: capture_keystroke and the keymap keep using Modifiers::core() without it, so Ctrl+Alt bindings behave as before.

Windows

keyboard.rs sets it when Control and Alt are held, VK_RMENU is down and the key translates to a character with the actual keyboard state (ToUnicodeEx). Left Alt + Ctrl, or a key with no AltGr character, stays false. Other platforms report false.

Compat

Needs the nsh-compat PR that defaults unread modifier fields (link below) so the compat job builds.

Verification

  • cargo test -p nsh-js --lib, cargo test -p nsh-core --lib keymap, cargo check for Linux, TUI, Web and GPUI crates pass locally.
  • Not yet run on Windows: no German-layout AltGr+Q vs Ctrl+Alt+; check in the embedded Windows, and the Windows, macOS, iOS and Android crates are compiled by CI only.

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

Windows sets it when the key's character comes from the layout's AltGr
level (Control and Alt held, Right Alt down, the key translates), so a
listener can tell AltGr text from a Ctrl+Alt shortcut, as the DOM's
getModifierState("AltGraph") does. Shortcuts never name or match it.

Fixes #2936

Agent: rust-maintainer
@huacnlee

Copy link
Copy Markdown
Member Author

Compat dependency: longbridge/nsh-compat#69

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

Agent: rust-maintainer
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

Copy link
Copy Markdown
Member Author

💡 Re-review of the altGraph API: where it comes from and whether it fits.

Source. The name and meaning come from the DOM / W3C UI Events: KeyboardEvent.getModifierState("AltGraph") ("AltGraph" is in the UI Events Key spec's modifier list). Chromium and Firefox on Windows report ctrlKey and altKey as true for AltGr and AltGraph as true, which is exactly the shape this PR copies. There is no GPUI precedent: GPUI's Modifiers is control / alt / shift / platform / function, and winit, AppKit (NSEventModifierFlags) and UIKit have no AltGr flag either (on macOS, Option plays that role). Qt's closest is GroupSwitchModifier (X11 only). So the precedent is Web-only, which fits NSH's "JS-facing APIs follow Web APIs" rule; the camelCase altGraph next to GPUI-named control/alt/platform is the one mixed vocabulary, but it is the DOM's own word, so I'd keep it.

Is it needed? Yes for #2936: an app's onKeyDown cannot otherwise tell AltGr text from a Ctrl+Alt shortcut on Windows, and NSH can't solve this inside the framework because the decision is the app's. Clearing control/alt instead would diverge from browsers and break real Ctrl+Alt handling.

Problems to fix before merge

  1. Web reports false even though the browser gives us the real value. Since the API is literally getModifierState("AltGraph"), nsh-platform-web should forward it from the DOM event.
  2. Linux reports false. GTK exposes the AltGr level (ISO_Level3_Shift / gdk::ModifierType level-3 state); European layouts on Linux hit the same problem. Either wire it or say "Windows and Web" explicitly in the docs.
  3. Doc semantics drift from the DOM. The doc says "the key's character came from the layout's AltGr level"; the DOM means "the AltGr key is held". The Windows implementation additionally requires a translated character, so AltGr + a key with no AltGr level reports false. That is arguably more useful, but the doc should state it as NSH's definition instead of attributing it to the DOM.
  4. Rust breaking change. Modifiers has public fields, so every struct literal outside the crate breaks (this PR had to touch ~25 call sites, plus nsh-compat). Fine for 0.x, but it should be listed under Breaking Changes in the next release notes; consider ..Default::default() in examples.
  5. Mouse/press events share Modifiers and will always carry altGraph: false. Acceptable (the DOM's MouseEvent.getModifierState behaves the same), just document "key events only" in the TS doc as the Rust doc already does.

Verdict: the API shape is reasonable and has a clear source; fix 1 and 3 (and 2 or document it) before merging. Native Windows check (German AltGr+Q vs Ctrl+Alt+;) is still not done.

🤖 Noop (lead) · AI agent acting for @huacnlee

@huacnlee
huacnlee merged commit b1250b8 into main Nov 23, 2026
10 of 24 checks passed
@huacnlee
huacnlee deleted the agent/T-10919-altgraph branch November 23, 2026 16:04
huacnlee added a commit that referenced this pull request Nov 23, 2026
#2979 added `alt_graph: false` to `synth.rs`, but that literal builds
the GPUI platform's internal `input::Modifiers` (Windows-only path),
which has no such field. Windows Clippy on main fails with E0560. This
removes the stray line.

Verification: only compiled on Windows CI (the path is `cfg(windows)`).

🤖 [Noop](https://huacnlee.github.io/noop) (rust-maintainer) · AI agent
acting for @huacnlee

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
huacnlee added a commit that referenced this pull request Nov 23, 2026
…2990)

## Description

#2979 gave the Windows test agent's key presses `alt_graph: false`
(`agent.rs`), a field of the core's `keymap::Modifiers`, on the
platform's own `input::Modifiers`, which has no such field.
`nsh-platform-windows` no longer builds with `dev`, which compiles the
test agent, so `nsh run` / `nsh test` on Windows fail from a checkout:

```
error[E0560]: struct `input::Modifiers` has no field named `alt_graph`
   --> crates\nsh-platform-windows\src\agent.rs:289:13
```

`cargo clippy -p nsh-platform-windows` without features does not compile
`agent.rs`, so it passes. The line goes: the platform's modifiers become
the core's through `Modifiers::core()`, which already sets `alt_graph:
false`.

## How to Test

On Windows:

- `cargo clippy -p nsh-platform-windows -p nsh-windows-host
--all-targets --features nsh-platform-windows/dev`: it fails before this
change and is clean after.
- `cargo build -p nsh-cli -p nsh-windows-host`, then `./target/debug/nsh
test examples/tasks/App.tsx --target windows` with steps on stdin
(typing, keyboard presses through the agent): it runs.

## Checklist

- [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and
followed the guidelines.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
madcodelife added a commit that referenced this pull request Nov 23, 2026
…2988)

Since #2979, `nsh-platform-windows` no longer compiles:

```
error[E0560]: struct `input::Modifiers` has no field named `alt_graph`
   --> crates\nsh-platform-windows\src\agent.rs:289:13
    = note: all struct fields are already assigned
```

The test agent's `press_keys` builds the Windows platform's own
`input::Modifiers` (shift / ctrl / alt / meta), which has no
`alt_graph`; only the core `keymap::Modifiers` gained it, and
`input::Modifiers::to_core` already fills it with `false`. #2979's own
Windows check was skipped, so the error first showed on PR merge refs
(e.g. #2980's Windows `check`, run 45214662517).

This drops the stray field. Synthesized key presses carry no AltGr
state, as before.

## Public API
None.

## Verification
Not compiled locally; the Windows `check` on this PR verifies the build.
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.

Windows: KeyEvent can't tell AltGr text from a Ctrl+Alt shortcut (no AltGraph)

2 participants