New scheme for keyboard logical key ID (#85121)

This PR updates the ID used by logical keyboard keys.

The logical key ID is still composed of 2 parts: 32 bits of value, and 8 bits of plane. But the assignment of planes has been drastically changed. HID plane is removed, and unprintable plane and Flutter plane are added. This is to reflect the new generation method for logical key IDs. Now keys that are defined by Flutter but not by dom_key_data are placed into the Flutter plane, including numpad keys, sided modifier keys, and gamepad keys. The values for platform planes have also been adjusted.

The generation script and README have been updated accordingly as well.

A new file, test_utils/key_codes.h is now generated to assist engine unit testing.

All lists generated by the script are now sorted by the key.
This commit is contained in:
Tong Mu
2021-07-14 13:07:22 -07:00
committed by GitHub
parent 252f6a0285
commit c8d0b1d9db
38 changed files with 4814 additions and 4724 deletions

View File

@@ -43,7 +43,7 @@ use `--engine-root=/ENGINE/GCLIENT/ROOT` to specify the engine root.
Other options can be found using `--help`.
## Key Code ID Scheme
## Logical Key ID Scheme
To provide logical keys with unique ID codes, Flutter uses a scheme
to assign logical keycodes which keeps us out of the business of minting new
@@ -58,109 +58,76 @@ the API.
However, if you are porting Flutter to a new platform, you should follow the
following guidelines for specifying logical key codes.
The logical key code is a 37-bit integer in a namespace that we control and
define. It has values in the following ranges.
The logical key code is a 52-bit integer (due to the limitation of JavaScript).
The entire namespace is divided into 32-bit *planes*. The upper 20 bits of the
ID represent the plane ID, while the lower 32 bits represent values in the
plane. For example, plane 0x1 refers to the range 0x1 0000 0000 -
0x1 FFFF FFFF. Each plane manages how the values within the range are assigned.
- **0x00 0000 0000 - 0x0 0010 FFFF**: For keys that generate Unicode
The planes are planned as follows:
- **Plane 0x00**: The Unicode plane. This plane contains keys that generate Unicode
characters when pressed (this includes dead keys, but not e.g. function keys
or shift keys), the logical key code is the Unicode code point corresponding
to the representation of the key in the current keyboard mapping. The
Unicode code point might not match the string that is generated for
an unshifted keypress of that key, for example, we would use U+0034 for the
“4 \$” key in the US layout, and also the “4 ;” key in the Russian layout,
and also, maybe less intuitively, for the “' 4 {“ in French layout (wherein
the latter case, an unshifted press gets you a ', not a 4). Similarly, the Q
key in the US layout outputs a q in normal usage, but its code would be 0x0
0000 0051 (U+00051 being the code for the uppercase Q).
or shift keys). The value is defined as the Unicode code point corresponding
to the character, lower case and without modifier keys if possible.
Examples are Key A (0x61), Digit 1 (0x31), Colon (0x3A), and Key Ù (0xD9).
(The "Colon" key represents a keyboard key that prints the ":"
character without modifiers, which can be found on the French layout. On the
US layout, the key that prints ":" is the Semicolon key.)
This plane also contains key None (0x0).
- **0x01 0000 0000 - 0x01 FFFF FFFF**: For keys that are defined by the [USB HID
standard](https://www.usb.org/sites/default/files/documents/hut1_12v2.pdf),
the key code consists of the 32 bit USB extended usage code. For
example, the Enter key would have code 0x01 0007 0028. Only keys that fall
into collections "Keyboard", "Keypad", and "Tablet PC System Controls" are
considered for this API; for example, a mixing desk with multiple
collections of volume controls would not be exposed via DOWN and UP events,
nor would a mouse, joystick, or golf simulator control.
- **Plane 0x01**: The unprintable plane. This plane contains keys that are defined
by the [Chromium key list](https://chromium.googlesource.com/codesearch/chromium/src/+/refs/heads/master/ui/events/keycodes/dom/dom_key_data.inc)
and do not generate Unicode characters. The value is defined as the macro
value defined by the Chromium key list. Examples are CapsLock (0x105),
ArrowUp (0x304), F1 (0x801), Hiragata (0x716), and TVPower (0xD4B).
Some keys that exist in the Chromium key list are not present in Flutter in this plane, most notably
modifiers keys (such as Shift). See the Flutter plane below for more
information.
- **0x02 0000 0000 - 0xFF FFFF FFFF**: For keys that aren't defined in USB at the
time of implementation, but that we need to support. For example, if Flutter
were ever ported to the Symbolics LM-2, the "thumb up" key might be given
the code 0x14 0000 0001, where 0x14 is defined as the “Symbolics” platform
range. Where possible, we will use specific subranges of this space to reuse
keys from other platforms. When this is not possible, the prefix 0xFF is
reserved for “Custom” codes. Each platform from which we take codes will get
a unique prefix in the range 0x2-0xFE. If multiple systems define keys with
the same usage (not the same number), then the value with the lowest prefix
is used as the defining code.
- **Plane 0x02**: The Flutter plane. This plane contains keys that are
defined by Flutter. The values are also manually assigned by Flutter.
Modifier keys are placed in this plane, because Flutter distinguishes
between sided modifier keys (for example "ShiftLeft" and "ShiftRight"),
while the web doesn't (only has "Shift").
Other examples are numpad keys and gamepad keys.
Prefixes will be:
- **Plane 0x03-0x0F**: Reserved.
- **Plane 0x10-0x1F**: Platform planes managed by Flutter. Each platform plane
corresponds to a Flutter embedding officially supported by Flutter. The
platforms are listed as follows:
| Code | Platform |
| ---- | -------- |
| 0x02 | Android |
| 0x03 | Fuchsia |
| 0x04 | iOS |
| 0x05 | macOS |
| 0x06 | Linux |
| 0x07 | Windows |
| 0x08 | Web |
| 0xFF | Custom |
| 0x11 | Android |
| 0x12 | Fuchsia |
| 0x13 | iOS |
| 0x14 | macOS |
| 0x15 | Gtk |
| 0x16 | Windows |
| 0x17 | Web |
| 0x18 | GLFW |
Further ranges will be added as platforms are added. The platform prefix
does not define the platform it is used on, it is just the platform that
decides what the value is: the codes are mapped to the same value on all
platforms.
Platform planes store keys that are private to the Flutter embedding of this
platform. This most likely means that these keys have not been officially
recognized by Flutter.
- **0x100 0000 0000 - 0x1FF FFFF FFFF**: For keys that have no definition yet in
Flutter, but that are encountered in the field, this range is used to embed
the platform-specific keycode in an ID that must be tested for in a
platform-specific way. For instance, if a platform generates a new USB
HID code 0x07 00E8 that a Flutter app wasn’t compiled with, then it would
appear in the app as 0x100 0007 00E8, and the app could test against that
code. Yes, this also means that once they recompile with a version of
Flutter that supports this new HID code, apps looking for this code will
break. This situation is only meant to provide a fallback ability for apps
to handle esoteric codes that their version of Flutter doesn’t support yet.
The prefix for this code is the platform prefix from the previous sections,
plus 0x100.
The value scheme within a platform plane is decided by the platform,
typically using the field from the platform's native key event that
represents the key's logical effect (such as `keycode`, `virtual key`, etc).
- **0x200 0000 0000 - 0x2FF FFFF FFFF**: For pseudo-keys which represent
combinations of other keys, and conceptual keys which don't have a physical
representation. This is where things like key synonyms are defined (e.g.
"shiftLeft" is a synonym for "shift": the "shift" key is a pseudo-key
representing either the left or right shift key).
In time, keys that originally belong to a platform plane might be added to
Flutter, especially if a key is found shared by multiple platforms. The values
of that key will be changed to a new value within the Flutter plane, and all
platforms managed by Flutter will start to send the new value, making it a
breaking change. Therefore, when handling an unrecognized key on a platform
managed by Flutter, it is recommended to file a new issue to add this value
to `keyboard_key.dart` instead of using the platform-plane value. However,
for a custom platform (see below), since the platfrom author has full control
over key mapping, such change will not cause breakage and it is recommended
to use the platform-plane value to avoid adding platform-exclusive values
to the framework.
**This is intended to get us out of the business of defining key codes where
possible.** We still have to have mapping tables, but at least the actual minting
of codes is deferred to other organizations to a large extent. Coming up with a
code is a mechanical process consisting of just picking the lowest number code
possible that matches the semantic meaning of the key according to the
definitions above.
Here are some examples:
For example, on a French keyboard layout, pressing CAPS LOCK then pressing
SHIFT + Y would generate the following sequence:
DOWN, code 0x0100070039. (CAPS LOCK DOWN)<br>
UP, code 0x0100070039. (CAPS LOCK UP)<br>
DOWN, code 0x01000700E1 (LEFT SHIFT DOWN)<br>
DOWN, code 0x0000000059, string U+00059 (Y DOWN)<br>
UP, code 0x0000000059 (Y UP)<br>
UP, code 0x01000700E1 (LEFT SHIFT UP)<br>
Here's another example. On a German keyboard layout, you press ^e (the ^ key is
at the top left of the keyboard and is a dead key) to produce an “ê”:
DOWN, code 0x0000000302 (CIRCUMFLEX DOWN) It produces no string, because it's a dead
key. The key code is for "Combining circumflex accent U+0302" in Unicode.<br>
UP, code 0x0000000302 (CIRCUMFLEX UP)<br>
DOWN, code 0x0000000065, string U+000EA (Unicode for ê‬) (E DOWN).<br>
UP, code 0x0000000065. (E UP).<br>
It is an important point that even though we’re representing many keys with USB
HID codes, these are not necessarily the same HID codes produced by the hardware
and presented to the driver, since on most platforms we have to map the platform
representation back to an HID code because we don’t have access to the original
HID code. USB HID is simply a conveniently well-defined standard that includes
many of the keys we would want.
- **Plane 0x20-0x2F**: Custom platform planes. Similar to Flutter's platform
planes, but for private use by custom platforms.