README.md 6.06 KB
Newer Older
1 2 3
## Keycode Generator

This directory contains a keycode generator that can generate Dart code for
4
the `LogicalKeyboardKey` and `PhysicalKeyboardKey` classes.
5

6
It generates multiple files across Flutter. For framework, it generates
7

8 9
* [`keyboard_key.g.dart`](../../../packages/flutter/lib/src/services/keyboard_key.g.dart), which contains the definition and list of logical keys and physical keys; and
* [`keyboard_maps.g.dart`](../../../packages/flutter/lib/src/services/keyboard_maps.g.dart), which contains platform-specific immutable maps used for the `RawKeyboard` API.
10

11 12
For engine, it generates one key mapping file for each platform, as well as some
files for testing purposes.
13 14

It draws information from various source bases, including online
15
repositories, and manual mapping in the `data` subdirectory. It incorporates
16
this information into a giant list of physical keys
17
([`physical_key_data.g.json`](data/physical_key_data.g.json)),
18
and another for logical keys
19
([`logical_key_data.g.json`](data/logical_key_data.g.json)).
20 21
The two files are checked in, and can be used as the data source next time so that
output files can be generated without the Internet.
22 23

## Running the tool
24

25
The tool can be run based on the existing database. To do this, run:
26 27

```bash
28
/PATH/TO/ROOT/bin/gen_keycodes
29 30
```

31 32 33 34 35 36
The tool can also be run by rebuilding the database by drawing online information
anew before generating the files. To do this, run:

```bash
/PATH/TO/ROOT/bin/gen_keycodes --collect
```
37

38
This will generate `physical_key_data.g.json` and `logical_key_data.g.json`. These
39
files should be checked in.
40

41
By default this tool assumes that the gclient directory for flutter/engine
42
and the root for the flutter/flutter are placed at the same folder. If not,
43
use `--engine-root=/ENGINE/GCLIENT/ROOT` to specify the engine root.
44

45
Other options can be found using `--help`.
46

47
## Key ID Scheme
48

49 50 51
To provide keys with unique ID codes, Flutter uses a scheme to assign keycodes
which keeps us out of the business of minting new codes ourselves. This applies
both logical keys and physical keys.
52

53 54 55
The codes are meant to be opaque to the user, and should never be unpacked for
meaning, since the coding scheme could change at any time and the meaning is
likely to be retrievable more reliably and correctly from the API.
56 57

However, if you are porting Flutter to a new platform, you should follow the
58
following guidelines for specifying key codes.
59

60 61 62 63 64
The 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.
65

66 67
The planes are planned as follows:

68 69 70 71 72 73 74 75 76 77 78 79 80
- **Plane 0x00**: The Unicode plane. For logical keys, this plane contains keys
  that generate Unicode characters when pressed (this includes dead keys, but
  not e.g. function keys 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.) For physical
  keys, this plane contains keys from USB HID usages.

- **Plane 0x01**: The unprintable plane. This plane contains logical 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)
81 82 83 84 85 86 87 88
  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.

- **Plane 0x02**: The Flutter plane. This plane contains keys that are
89 90 91 92
  defined 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.
93 94 95 96 97 98

- **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:
99 100 101

  | Code | Platform |
  | ---- | -------- |
102 103 104 105 106 107 108 109 110 111 112 113 114 115
  | 0x11 | Android  |
  | 0x12 | Fuchsia  |
  | 0x13 | iOS      |
  | 0x14 | macOS    |
  | 0x15 | Gtk      |
  | 0x16 | Windows  |
  | 0x17 | Web      |
  | 0x18 | GLFW     |

  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.

  The value scheme within a platform plane is decided by the platform,
116
  typically using the other fields from the platform's native key event.
117 118 119 120 121 122 123

  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
124
  to `keyboard_key.g.dart` instead of using the platform-plane value. However,
125
  for a custom platform (see below), since the platform author has full control
126 127 128 129
  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.

130
- **Plane 0x20-0x2F**: Custom platform planes. Similar to Flutter's platform
131
  planes, but for private use by custom platforms.