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

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -152,7 +152,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData {
@override
PhysicalKeyboardKey get physicalKey {
if (kAndroidToPhysicalKey.containsKey(scanCode)) {
return kAndroidToPhysicalKey[scanCode] ?? PhysicalKeyboardKey.none;
return kAndroidToPhysicalKey[scanCode]!;
}
// Android sends DPAD_UP, etc. as the keyCode for joystick DPAD events, but
@@ -174,7 +174,7 @@ class RawKeyEventDataAndroid extends RawKeyEventData {
return PhysicalKeyboardKey.arrowRight;
}
}
return PhysicalKeyboardKey.none;
return PhysicalKeyboardKey(LogicalKeyboardKey.androidPlane + scanCode);
}
@override
@@ -198,16 +198,12 @@ class RawKeyEventDataAndroid extends RawKeyEventData {
}
// Look to see if the keyCode is one we know about and have a mapping for.
LogicalKeyboardKey? newKey = kAndroidToLogicalKey[keyCode];
final LogicalKeyboardKey? newKey = kAndroidToLogicalKey[keyCode];
if (newKey != null) {
return newKey;
}
// This is a non-printable key that we don't know about, so we mint a new
// code with the autogenerated bit set.
const int androidKeyIdPlane = 0x00200000000;
newKey ??= LogicalKeyboardKey(androidKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask);
return newKey;
return LogicalKeyboardKey(keyCode | LogicalKeyboardKey.androidPlane);
}
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {

View File

@@ -73,20 +73,18 @@ class RawKeyEventDataFuchsia extends RawKeyEventData {
}
// Look to see if the hidUsage is one we know about and have a mapping for.
LogicalKeyboardKey? newKey = kFuchsiaToLogicalKey[hidUsage | LogicalKeyboardKey.hidPlane];
final LogicalKeyboardKey? newKey = kFuchsiaToLogicalKey[hidUsage | LogicalKeyboardKey.fuchsiaPlane];
if (newKey != null) {
return newKey;
}
// This is a non-printable key that we don't know about, so we mint a new
// code with the autogenerated bit set.
const int fuchsiaKeyIdPlane = 0x00300000000;
newKey ??= LogicalKeyboardKey(fuchsiaKeyIdPlane | hidUsage | LogicalKeyboardKey.autogeneratedMask);
return newKey;
// code.
return LogicalKeyboardKey(hidUsage | LogicalKeyboardKey.fuchsiaPlane);
}
@override
PhysicalKeyboardKey get physicalKey => kFuchsiaToPhysicalKey[hidUsage] ?? PhysicalKeyboardKey.none;
PhysicalKeyboardKey get physicalKey => kFuchsiaToPhysicalKey[hidUsage] ?? PhysicalKeyboardKey(LogicalKeyboardKey.fuchsiaPlane + hidUsage);
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {
if (modifiers & anyMask == 0) {

View File

@@ -90,7 +90,7 @@ class RawKeyEventDataIos extends RawKeyEventData {
String get keyLabel => charactersIgnoringModifiers;
@override
PhysicalKeyboardKey get physicalKey => kIosToPhysicalKey[keyCode] ?? PhysicalKeyboardKey.none;
PhysicalKeyboardKey get physicalKey => kIosToPhysicalKey[keyCode] ?? PhysicalKeyboardKey(LogicalKeyboardKey.iosPlane + keyCode);
@override
LogicalKeyboardKey get logicalKey {
@@ -137,22 +137,9 @@ class RawKeyEventDataIos extends RawKeyEventData {
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
}
// Control keys like "backspace" and movement keys like arrow keys don't
// have a printable representation, but are present on the physical
// keyboard. Since there is no logical keycode map for iOS (iOS uses the
// keycode to reference physical keys), a LogicalKeyboardKey is created with
// the physical key's HID usage and debugName. This avoids duplicating the
// physical key map.
if (physicalKey != PhysicalKeyboardKey.none) {
final int keyId = physicalKey.usbHidUsage | LogicalKeyboardKey.hidPlane;
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
}
// This is a non-printable key that is unrecognized, so a new code is minted
// with the autogenerated bit set.
const int iosKeyIdPlane = 0x00400000000;
return LogicalKeyboardKey(iosKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask);
// This is a non-printable key that we don't know about, so we mint a new
// code.
return LogicalKeyboardKey(keyCode | LogicalKeyboardKey.iosPlane);
}
/// Returns true if the given label represents an unprintable key.

View File

@@ -73,7 +73,7 @@ class RawKeyEventDataLinux extends RawKeyEventData {
String get keyLabel => unicodeScalarValues == 0 ? '' : String.fromCharCode(unicodeScalarValues);
@override
PhysicalKeyboardKey get physicalKey => kLinuxToPhysicalKey[scanCode] ?? PhysicalKeyboardKey.none;
PhysicalKeyboardKey get physicalKey => kLinuxToPhysicalKey[scanCode] ?? PhysicalKeyboardKey(LogicalKeyboardKey.webPlane + scanCode);
@override
LogicalKeyboardKey get logicalKey {
@@ -96,17 +96,14 @@ class RawKeyEventDataLinux extends RawKeyEventData {
}
// Look to see if the keyCode is one we know about and have a mapping for.
LogicalKeyboardKey? newKey = keyHelper.logicalKey(keyCode);
final LogicalKeyboardKey? newKey = keyHelper.logicalKey(keyCode);
if (newKey != null) {
return newKey;
}
const int linuxKeyIdPlane = 0x00600000000;
// This is a non-printable key that we don't know about, so we mint a new
// code with the autogenerated bit set.
newKey ??= LogicalKeyboardKey(linuxKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask);
return newKey;
// code.
return LogicalKeyboardKey(keyCode | keyHelper.platformPlane);
}
@override
@@ -157,6 +154,9 @@ abstract class KeyHelper {
/// The logical key from the specific key code mapping.
LogicalKeyboardKey? logicalKey(int keyCode);
/// The platform plane mask value of this platform.
int get platformPlane;
}
/// Helper class that uses GLFW-specific key mappings.
@@ -294,6 +294,9 @@ class GLFWKeyHelper implements KeyHelper {
LogicalKeyboardKey? logicalKey(int keyCode) {
return kGlfwToLogicalKey[keyCode];
}
@override
int get platformPlane => LogicalKeyboardKey.glfwPlane;
}
/// Helper class that uses GTK-specific key mappings.
@@ -432,4 +435,7 @@ class GtkKeyHelper implements KeyHelper {
LogicalKeyboardKey? logicalKey(int keyCode) {
return kGtkToLogicalKey[keyCode];
}
@override
int get platformPlane => LogicalKeyboardKey.gtkPlane;
}

View File

@@ -65,7 +65,7 @@ class RawKeyEventDataMacOs extends RawKeyEventData {
String get keyLabel => charactersIgnoringModifiers;
@override
PhysicalKeyboardKey get physicalKey => kMacOsToPhysicalKey[keyCode] ?? PhysicalKeyboardKey.none;
PhysicalKeyboardKey get physicalKey => kMacOsToPhysicalKey[keyCode] ?? PhysicalKeyboardKey(LogicalKeyboardKey.windowsPlane + keyCode);
@override
LogicalKeyboardKey get logicalKey {
@@ -106,22 +106,9 @@ class RawKeyEventDataMacOs extends RawKeyEventData {
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
}
// Control keys like "backspace" and movement keys like arrow keys don't
// have a printable representation, but are present on the physical
// keyboard. Since there is no logical keycode map for macOS (macOS uses the
// keycode to reference physical keys), a LogicalKeyboardKey is created with
// the physical key's HID usage and debugName. This avoids duplicating the
// physical key map.
if (physicalKey != PhysicalKeyboardKey.none) {
final int keyId = physicalKey.usbHidUsage | LogicalKeyboardKey.hidPlane;
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
}
// This is a non-printable key that is unrecognized, so a new code is minted
// with the autogenerated bit set.
const int macOsKeyIdPlane = 0x00500000000;
return LogicalKeyboardKey(macOsKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask);
// This is a non-printable key that we don't know about, so we mint a new
// code.
return LogicalKeyboardKey(keyCode | LogicalKeyboardKey.macosPlane);
}
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {

View File

@@ -60,7 +60,7 @@ class RawKeyEventDataWeb extends RawKeyEventData {
@override
PhysicalKeyboardKey get physicalKey {
return kWebToPhysicalKey[code] ?? PhysicalKeyboardKey.none;
return kWebToPhysicalKey[code] ?? PhysicalKeyboardKey(LogicalKeyboardKey.webPlane + code.hashCode);
}
@override
@@ -80,9 +80,8 @@ class RawKeyEventDataWeb extends RawKeyEventData {
}
// This is a non-printable key that we don't know about, so we mint a new
// code with the autogenerated bit set.
const int webKeyIdPlane = 0x00800000000;
return LogicalKeyboardKey(webKeyIdPlane | code.hashCode | LogicalKeyboardKey.autogeneratedMask);
// code.
return LogicalKeyboardKey(code.hashCode | LogicalKeyboardKey.webPlane);
}
@override

View File

@@ -60,7 +60,7 @@ class RawKeyEventDataWindows extends RawKeyEventData {
String get keyLabel => characterCodePoint == 0 ? '' : String.fromCharCode(characterCodePoint);
@override
PhysicalKeyboardKey get physicalKey => kWindowsToPhysicalKey[scanCode] ?? PhysicalKeyboardKey.none;
PhysicalKeyboardKey get physicalKey => kWindowsToPhysicalKey[scanCode] ?? PhysicalKeyboardKey(LogicalKeyboardKey.windowsPlane + scanCode);
@override
LogicalKeyboardKey get logicalKey {
@@ -81,16 +81,14 @@ class RawKeyEventDataWindows extends RawKeyEventData {
return LogicalKeyboardKey.findKeyByKeyId(keyId) ?? LogicalKeyboardKey(keyId);
}
// Look to see if the keyCode is one we know about and have a mapping for.
LogicalKeyboardKey? newKey = kWindowsToLogicalKey[keyCode];
final LogicalKeyboardKey? newKey = kWindowsToLogicalKey[keyCode];
if (newKey != null) {
return newKey;
}
// This is a non-printable key that we don't know about, so we mint a new
// code with the autogenerated bit set.
const int windowsKeyIdPlane = 0x00700000000;
newKey ??= LogicalKeyboardKey(windowsKeyIdPlane | keyCode | LogicalKeyboardKey.autogeneratedMask);
return newKey;
// code.
return LogicalKeyboardKey(keyCode | LogicalKeyboardKey.windowsPlane);
}
bool _isLeftRightModifierPressed(KeyboardSide side, int anyMask, int leftMask, int rightMask) {

View File

@@ -50,14 +50,13 @@ void main() {
expect(LogicalKeyboardKey.isControlCharacter('\xa0'), isFalse); // NO-BREAK SPACE
});
test('Control characters are not using incorrect values', () async {
const int kUnprintablePlane = 0x01000000000;
// Check some common control characters to make sure they're using
// their char code values, and not something else.
expect(LogicalKeyboardKey.backspace.keyId, equals(kUnprintablePlane + 0x08));
expect(LogicalKeyboardKey.tab.keyId, equals(kUnprintablePlane + 0x09));
expect(LogicalKeyboardKey.enter.keyId, equals(kUnprintablePlane + 0x0d));
expect(LogicalKeyboardKey.escape.keyId, equals(kUnprintablePlane + 0x1b));
expect(LogicalKeyboardKey.delete.keyId, equals(kUnprintablePlane + 0x7f));
expect(LogicalKeyboardKey.backspace.keyId, equals(LogicalKeyboardKey.unprintablePlane + 0x08));
expect(LogicalKeyboardKey.tab.keyId, equals(LogicalKeyboardKey.unprintablePlane + 0x09));
expect(LogicalKeyboardKey.enter.keyId, equals(LogicalKeyboardKey.unprintablePlane + 0x0d));
expect(LogicalKeyboardKey.escape.keyId, equals(LogicalKeyboardKey.unprintablePlane + 0x1b));
expect(LogicalKeyboardKey.delete.keyId, equals(LogicalKeyboardKey.unprintablePlane + 0x7f));
});
test('Basic synonyms can be looked up.', () async {
expect(LogicalKeyboardKey.shiftLeft.synonyms.first, equals(LogicalKeyboardKey.shift));