Unverified Commit fe052394 authored by b-luk's avatar b-luk Committed by GitHub

Expose keyCode field in RawKeyEventDataWeb. (#96483)

parent 5ac97140
......@@ -300,6 +300,7 @@ abstract class RawKeyEvent with Diagnosticable {
key: key ?? '',
location: message['location'] as int? ?? 0,
metaState: message['metaState'] as int? ?? 0,
keyCode: message['keyCode'] as int? ?? 0,
);
}
......
......@@ -32,6 +32,7 @@ class RawKeyEventDataWeb extends RawKeyEventData {
required this.key,
this.location = 0,
this.metaState = modifierNone,
this.keyCode = 0,
}) : assert(code != null),
assert(metaState != null);
......@@ -80,6 +81,12 @@ class RawKeyEventDataWeb extends RawKeyEventData {
/// * [isMetaPressed], to see if a META key is pressed.
final int metaState;
/// The `KeyboardEvent.keyCode` corresponding to this event.
///
/// See <https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/keyCode>
/// for more information.
final int keyCode;
@override
String get keyLabel => key == 'Unidentified' ? '' : _unicodeChar(key) ?? '';
......@@ -156,6 +163,7 @@ class RawKeyEventDataWeb extends RawKeyEventData {
properties.add(DiagnosticsProperty<String>('key', key));
properties.add(DiagnosticsProperty<int>('location', location));
properties.add(DiagnosticsProperty<int>('metaState', metaState));
properties.add(DiagnosticsProperty<int>('keyCode', keyCode));
}
@override
......@@ -168,7 +176,8 @@ class RawKeyEventDataWeb extends RawKeyEventData {
&& other.code == code
&& other.key == key
&& other.location == location
&& other.metaState == metaState;
&& other.metaState == metaState
&& other.keyCode == keyCode;
}
@override
......@@ -177,6 +186,7 @@ class RawKeyEventDataWeb extends RawKeyEventData {
key,
location,
metaState,
keyCode,
);
// Modifier key masks.
......
......@@ -2547,11 +2547,13 @@ void main() {
'key': 'a',
'location': 0,
'metaState': 0x0,
'keyCode': 0x41,
});
final RawKeyEventDataWeb data = keyAEvent.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA));
expect(data.logicalKey, equals(LogicalKeyboardKey.keyA));
expect(data.keyLabel, equals('a'));
expect(data.keyCode, equals(0x41));
});
test('Upper letter keys are correctly translated', () {
......@@ -2562,11 +2564,13 @@ void main() {
'key': 'A',
'location': 0,
'metaState': 0x1, // Shift
'keyCode': 0x41,
});
final RawKeyEventDataWeb data = keyAEvent.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.keyA));
expect(data.logicalKey, equals(LogicalKeyboardKey.keyA));
expect(data.keyLabel, equals('A'));
expect(data.keyCode, equals(0x41));
});
test('Control keyboard keys are correctly translated', () {
......@@ -2577,11 +2581,13 @@ void main() {
'key': 'Escape',
'location': 0,
'metaState': 0x0,
'keyCode': 0x1B,
});
final RawKeyEventDataWeb data = escapeKeyEvent.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.escape));
expect(data.logicalKey, equals(LogicalKeyboardKey.escape));
expect(data.keyLabel, isEmpty);
expect(data.keyCode, equals(0x1B));
});
test('Modifier keyboard keys are correctly translated', () {
......@@ -2592,11 +2598,13 @@ void main() {
'key': 'Shift',
'location': 1,
'metaState': RawKeyEventDataWeb.modifierShift,
'keyCode': 0x10,
});
final RawKeyEventDataWeb data = shiftKeyEvent.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.shiftLeft));
expect(data.logicalKey, equals(LogicalKeyboardKey.shiftLeft));
expect(data.keyLabel, isEmpty);
expect(data.keyCode, equals(0x10));
});
test('Arrow keys from a keyboard give correct physical key mappings', () {
......@@ -2607,11 +2615,13 @@ void main() {
'key': 'ArrowDown',
'location': 0,
'metaState': 0x0,
'keyCode': 0x28,
});
final RawKeyEventDataWeb data = arrowKeyDown.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.arrowDown));
expect(data.logicalKey, equals(LogicalKeyboardKey.arrowDown));
expect(data.keyLabel, isEmpty);
expect(data.keyCode, equals(0x28));
});
test('Unrecognized keys are mapped to Web plane', () {
......@@ -2631,6 +2641,7 @@ void main() {
expect(data.logicalKey.keyId, greaterThan(0x01700000000));
expect(data.logicalKey.keyId, lessThan(0x01800000000));
expect(data.keyLabel, isEmpty);
expect(data.keyCode, equals(0));
});
test('data.toString', () {
......@@ -2641,8 +2652,9 @@ void main() {
'key': 'a',
'location': 2,
'metaState': 0x10,
'keyCode': 0x41,
}).data.toString(), equalsIgnoringHashCodes(
'RawKeyEventDataWeb#00000(code: KeyA, key: a, location: 2, metaState: 16)'));
'RawKeyEventDataWeb#00000(code: KeyA, key: a, location: 2, metaState: 16, keyCode: 65)'));
// Without location
expect(RawKeyEvent.fromMessage(const <String, Object?>{
......@@ -2651,8 +2663,9 @@ void main() {
'code': 'KeyA',
'key': 'a',
'metaState': 0x10,
'keyCode': 0x41,
}).data.toString(), equalsIgnoringHashCodes(
'RawKeyEventDataWeb#00000(code: KeyA, key: a, location: 0, metaState: 16)'));
'RawKeyEventDataWeb#00000(code: KeyA, key: a, location: 0, metaState: 16, keyCode: 65)'));
});
test('data.equality', () {
......@@ -2663,11 +2676,13 @@ void main() {
'key': 'a',
'location': 2,
'metaState': 0x10,
'keyCode': 0x41,
}).data, const RawKeyEventDataWeb(
key: 'a',
code: 'KeyA',
location: 2,
metaState: 0x10,
keyCode: 0x41
));
expect(RawKeyEvent.fromMessage(const <String, Object?>{
......@@ -2677,6 +2692,7 @@ void main() {
'key': 'a',
'location': 2,
'metaState': 0x10,
'keyCode': 0x41,
}).data, isNot(equals(const RawKeyEventDataWeb(code: 'KeyA', key: 'a'))));
});
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment