Skip to content

feat: add physical_key to keyboard events and PhysicalKeyboardKey - #7125

Draft
ndonkoHenri wants to merge 1 commit into
feat/keyboard-key-enumfrom
feat/physical-keyboard-key
Draft

ndonkoHenri wants to merge 1 commit into
feat/keyboard-key-enumfrom
feat/physical-keyboard-key

Conversation

@ndonkoHenri

@ndonkoHenri ndonkoHenri commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Stacked on #7123.

Problem

Keyboard events carry only the logical key, which follows the active layout. On a French AZERTY keyboard the key at the US W position types Z, so WASD controls (games, position-based shortcuts) break for those users, and there's no way to read where a key is.

Change

if e.physical_key == ft.PhysicalKeyboardKey.W:  # same key on AZERTY, where it types Z
    move_up()
  • Client: the page's keyboard_event and KeyboardListener's key_down/key_up/key_repeat also send physical_key, the key's USB HID usage code. Flutter's PhysicalKeyboardKey.debugName is null in release builds, so the code is what identifies the key. KeyboardListener now builds its three events in one place.
  • ft.PhysicalKeyboardKey(int, Enum): one member per Flutter PhysicalKeyboardKey (269), named like KeyboardKey after the US QWERTY layout (W, DIGIT_1, ARROW_LEFT), valued with the HID code; str() gives the member name.
  • Events: physical_key: Union[PhysicalKeyboardKey, int] on KeyboardEvent, KeyDownEvent, KeyUpEvent and KeyRepeatEvent; a key without a member is its raw code.
  • Generated: generate_keyboard_keys.py renders both enums into keyboard_key.py from the same Flutter file, so the existing CI --verify step covers both.
  • Docs: PhysicalKeyboardKey type page; the keyboard shortcuts cookbook page lists physical_key and has a WASD example.

Testing

  • A throwaway widget test (not committed) sent an AZERTY press (logical keyZ, physical keyW) through KeyboardListener: down, repeat and up each delivered {key: "Z", physical_key: 0x0007001A}.
  • Python decodes that payload to KeyboardKey.Z / PhysicalKeyboardKey.W, and an unknown code to a plain int.
  • Unit suite passes; generator --verify up to date; ruff and dart analyze clean.
  • Not run in a live desktop or web client yet; the page-level event change is a one-line addition checked with the analyzer only.

Summary by Sourcery

Add physical keyboard key support so applications can distinguish a key’s position from the character produced by the active layout.

New Features:

  • Add physical keyboard position data to page and keyboard-listener events through a new physical_key property.
  • Expose PhysicalKeyboardKey values for USB HID key usages, with unknown usages preserved as integers.

Enhancements:

  • Generate logical and physical keyboard key enums from Flutter’s definitions with duplicate validation.
  • Refactor keyboard listener event dispatch to include shared key metadata.

Documentation:

  • Document PhysicalKeyboardKey and demonstrate layout-independent WASD handling in the keyboard shortcuts guide.

Chores:

  • Export the new physical-key enum through the Python SDK and record the feature in the changelog.

Keyboard events carried only the logical key, which follows the active
layout: on a French AZERTY keyboard the key at the US "W" position types
"Z", so WASD controls in games broke for those users. Events now also
carry the physical key, the key's position on the keyboard.

What changes:
- Client: the page's `keyboard_event` and `KeyboardListener`'s
  `key_down`/`key_up`/`key_repeat` send `physical_key`, the key's USB HID
  usage code (`PhysicalKeyboardKey.usbHidUsage`). Flutter has no physical
  key name outside debug builds, so the code is what identifies the key.
  `KeyboardListener` builds the three events in one place.
- `ft.PhysicalKeyboardKey(int, Enum)` has one member per Flutter
  `PhysicalKeyboardKey` (269), named like `KeyboardKey` (`W`, `DIGIT_1`,
  `ARROW_LEFT`) after the US QWERTY layout and valued with the HID usage
  code; `str()` gives the member name.
- `physical_key: Union[PhysicalKeyboardKey, int]` on `KeyboardEvent`,
  `KeyDownEvent`, `KeyUpEvent` and `KeyRepeatEvent`: a member for known
  keys, the raw code otherwise.
- `generate_keyboard_keys.py` renders both enums into `keyboard_key.py`
  from the same Flutter file; `check_unique` guards both against repeated
  names or values.
- Docs: `PhysicalKeyboardKey` type page and sidebar entry; the keyboard
  shortcuts cookbook page lists `physical_key` and shows a WASD example;
  1.2.0 changelog entry.

Tested: a throwaway widget test (not committed) sent an AZERTY press
(logical keyZ, physical keyW) through KeyboardListener and got
`{key: Z, physical_key: 0x0007001A}` for down, repeat and up; Python
decodes that to `KeyboardKey.Z` / `PhysicalKeyboardKey.W` and an unknown
code to a plain int; unit suite passes; generator --verify up to date;
ruff and dart analyze clean. Not run in a live desktop/web client.
@ndonkoHenri
ndonkoHenri force-pushed the feat/physical-keyboard-key branch from 6301348 to 885eee5 Compare November 23, 2026 01:53
@ndonkoHenri
ndonkoHenri added this pull request to stack #7126 November 23, 2026 01:54
@cloudflare-workers-and-pages

cloudflare-workers-and-pages Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

Deploying flet-website-v2 with  Cloudflare Pages  Cloudflare Pages

Latest commit: 885eee5
Status: ✅  Deploy successful!
Preview URL: https://1e9cd6b7.flet-website-v2.pages.dev
Branch Preview URL: https://feat-physical-keyboard-key.flet-website-v2.pages.dev

View logs

This branch has not been deployed

No deployments
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