Repository navigation
feat: add KeyboardKey enum for keyboard event keys - #7123
Open
ndonkoHenri wants to merge 3 commits into
Open
ndonkoHenri wants to merge 3 commits into
ndonkoHenri wants to merge 3 commits into
Conversation
Keyboard handlers compared `e.key` with hand-typed labels such as "Arrow Right" or " ", where a typo fails silently. `ft.KeyboardKey` names every key Flutter reports, so handlers can write `e.key == ft.KeyboardKey.ARROW_RIGHT`. What changes: - `KeyboardKey(str, Enum)` has one member per Flutter `LogicalKeyboardKey` (444), valued with the key's `keyLabel`, the string the client sends. Members are strings, so comparisons with plain labels keep working, and `__str__` returns the label, so f-strings show "Arrow Right". - `key` on `KeyboardEvent`, `KeyDownEvent`, `KeyUpEvent` and `KeyRepeatEvent` is `Union[KeyboardKey, str]`: a member for known keys, a plain string for keys without one (e.g. letters of non-English layouts). - `from_dict` decodes a `Union` that includes an enum into the member when one has the value, else keeps the value; `Optional` unions of several types now keep all of them instead of only the first. - `.github/scripts/generate_keyboard_keys.py` renders `keyboard_key.py` from Flutter's `keyboard_key.g.dart` at the `.fvmrc` version through a Jinja template; CI runs it with `--verify` in the icons step. - Docs: `KeyboardKey` type page and sidebar entry, cookbook example on the keyboard shortcuts page; examples use the enum; 1.2.0 changelog entry. Tested: unit suite passes; generator --verify reports up to date; ruff clean.
`convert_value` recognized only `typing.Union`, so a field annotated with PEP 604 syntax, such as `KeyboardKey | str` or `Padding | None`, was not treated as a union on Python 3.10-3.13, where `X | Y` has the origin `types.UnionType`: `None` handling and enum conversion were skipped and the raw value was returned. Both origins are now treated as a union: `X | None` keeps `None` and converts the rest like `Optional[X]`, and `SomeEnum | T` gives the enum member when one has the value, else the value, like `Union[SomeEnum, T]`. Tested: `Union[M, str]`, `M | str`, `M | str | None`, `Optional[M]` and dataclass/list unions convert the same on Python 3.10, 3.12 and 3.14; unit suite passes; ruff clean.
Adds the PR link to the 1.2.0 KeyboardKey entry.
Deploying flet-website-v2 with
|
| Latest commit: |
cf68bd2
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://df250376.flet-website-v2.pages.dev |
| Branch Preview URL: | https://feat-keyboard-key-enum.flet-website-v2.pages.dev |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Keyboard handlers compare
e.keywith hand-typed labels:The labels aren't discoverable, and a typo such as
"ArrowRight"or"Space"silently never matches.Change
ft.KeyboardKeynames every key the client can report:LogicalKeyboardKey(444), valued with the key'skeyLabel, the string the client already sends. No Dart or protocol change.KeyboardKey(str, Enum)members are strings, soe.key == "Arrow Right"keeps working, and__str__returns the label, sof"Key: {e.key}"still showsArrow Right.keyonKeyboardEvent,KeyDownEvent,KeyUpEventandKeyRepeatEventisUnion[KeyboardKey, str]: a member for known keys, a plain string for keys without one (e.g."Ü"on a German layout).from_dict: aUnionthat includes an enum decodes to the member when one has the value, else keeps the value.Optionalunions of several types keep all of them (previously only the first), andX | Yunions are handled likeUnion[X, Y](on 3.10–3.13 they have a different origin and were skipped)..github/scripts/generate_keyboard_keys.pyrenderskeyboard_key.pyfrom Flutter'skeyboard_key.g.dartat the.fvmrcversion through a Jinja template. CI runs it with--verifyin the icons step.KeyboardKeytype page, a key-comparison example on the keyboard shortcuts cookbook page, and examples switched to the enum.Testing
generate_keyboard_keys.py --verifyreports up to date; ruff clean.from_dictunion handling checked on Python 3.10, 3.12 and 3.14 withUnion[M, str],M | str,M | str | None,Optional[M]and dataclass/list unions.from_dictdecoding that was checked.Summary by Sourcery
Add a string-compatible
KeyboardKeyenum and integrate it across keyboard events, deserialization, examples, documentation, and generated-file validation.New Features:
KeyboardKeyenum covering Flutter keyboard event labels, with string-compatible members for backward-compatible key comparisons.Bug Fixes:
Enhancements:
KeyboardKeyvalues while retaining existing string behavior.CI:
Documentation:
KeyboardKeyand add keyboard shortcut examples using enum members.