Skip to content

gpui: add the physical key code to Keystroke - #14

Open
Cyberistic wants to merge 2 commits into
remorses:gpuixfrom
Cyberistic:feat/keystroke-code
Open

Cyberistic wants to merge 2 commits into
remorses:gpuixfrom
Cyberistic:feat/keystroke-code

Conversation

@Cyberistic

@Cyberistic Cyberistic commented Oct 24, 2026 •

Copy link
Copy Markdown

Tested on mac only, need it for https://github.com/remorses/gpuix#105.

Adds the physical key code to Keystroke, so a binding or an app can match a key that does not move with the layout (KeyS on AZERTY is still the S position).

pub struct Keystroke {
    pub modifiers: Modifiers,
    pub key: String,
    pub key_char: Option<String>,
    /// The DOM `KeyboardEvent.code` spelling, layout-independent. `None` when
    /// the platform cannot report one.
    pub code: Option<String>,
}

`Keystroke::code` carries the layout-independent physical key in the DOM
`KeyboardEvent.code` spelling (`"KeyS"`, `"Digit1"`, `"ArrowLeft"`), or
`None` when the platform cannot report one. `key` keeps describing the
character the layout produced, so keybinding parsing, matching, and
`Display` are untouched; the field is `#[serde(default)]` so keystrokes
serialized before it existed still decode.

The mapping tables live in `gpui::keystroke_code` as pure functions, one
per platform, so every host can unit-test all of them:

- macOS: `NSEvent.keyCode` (`kVK_*`)
- Windows: the Set 1 scancode from `WM_KEYDOWN`, with the extended flag
- Linux: the xkb key code, i.e. the evdev code plus 8

The macOS, Windows, Linux, and web backends fill the field in where they
build a keystroke; synthesized keystrokes leave it `None`.

Unblocks remorses/gpuix#105.
@Cyberistic
Cyberistic marked this pull request as ready for review October 25, 2026 04:24
Checked every entry against Chromium's `dom_code_data.inc`:

- macOS `kVK_Help` (0x72) sits in the Insert position and browsers report it
  as `Insert`, not `Help`; JIS Eisu and Kana are `Lang2` and `Lang1`.
- Windows reports Pause as the plain scan code 0x45 and NumLock as the
  extended 0x45; the table had NumLock on the plain code and no Pause.
  Extended 0x37 is PrintScreen.
- evdev 128 (`KEY_STOP`) is `BrowserStop`; `MediaStop` is 166 (`KEY_STOPCD`).

The Windows table also gains the keys the other two already map:
NumpadEqual, NumpadComma, Lang1, Lang2, Power and the browser keys.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant