[RawKeyboard] Allow inconsistent modifiers for Web (#114499)

Co-authored-by: Bruno Leroux <bruno.leroux@gmail.com>
This commit is contained in:
Bruno Leroux 2022-11-07 21:41:09 +01:00 committed by GitHub
parent fe5eb2db75
commit 5628ebf662
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 67 additions and 1 deletions

View file

@ -209,7 +209,7 @@ class LogicalKeyboardKey extends KeyboardKey {
/// Auto-generated IDs should be a rare occurrence: Flutter supports most keys.
///
/// Keys that generate Unicode characters (even if unknown to Flutter) will
/// not return true for `isAutogenerated`, since they will be assigned a
/// not return true for [isAutogenerated], since they will be assigned a
/// Unicode-based code that will remain stable.
///
/// If Flutter adds support for a previously unsupported key code, the ID it

View file

@ -29,5 +29,6 @@
"Shift": ["ShiftLeft", "ShiftLeft", "ShiftRight", null],
"Control": ["ControlLeft", "ControlLeft", "ControlRight", null],
"Alt": ["AltLeft", "AltLeft", "AltRight", null],
"AltGraph": ["AltGraph", null, "AltGraph", null],
"Meta": ["MetaLeft", "MetaLeft", "MetaRight", null]
}

View file

@ -2755,6 +2755,7 @@ const Map<String, List<LogicalKeyboardKey?>> kWebLocationMap = <String, List<Log
'8': <LogicalKeyboardKey?>[LogicalKeyboardKey.digit8, null, null, LogicalKeyboardKey.numpad8],
'9': <LogicalKeyboardKey?>[LogicalKeyboardKey.digit9, null, null, LogicalKeyboardKey.numpad9],
'Alt': <LogicalKeyboardKey?>[LogicalKeyboardKey.altLeft, LogicalKeyboardKey.altLeft, LogicalKeyboardKey.altRight, null],
'AltGraph': <LogicalKeyboardKey?>[LogicalKeyboardKey.altGraph, null, LogicalKeyboardKey.altGraph, null],
'ArrowDown': <LogicalKeyboardKey?>[LogicalKeyboardKey.arrowDown, null, null, LogicalKeyboardKey.numpad2],
'ArrowLeft': <LogicalKeyboardKey?>[LogicalKeyboardKey.arrowLeft, null, null, LogicalKeyboardKey.numpad4],
'ArrowRight': <LogicalKeyboardKey?>[LogicalKeyboardKey.arrowRight, null, null, LogicalKeyboardKey.numpad6],

View file

@ -848,6 +848,12 @@ class RawKeyboard {
_keysPressed[event.physicalKey] = logicalKey;
}
}
// On Web, PhysicalKeyboardKey.altRight can be map to LogicalKeyboardKey.altGraph or
// LogicalKeyboardKey.altRight:
// https://github.com/flutter/flutter/issues/113836
if (event.data is RawKeyEventDataWeb && event.physicalKey == PhysicalKeyboardKey.altRight) {
_keysPressed[event.physicalKey] = event.logicalKey;
}
}
}

View file

@ -842,6 +842,64 @@ void main() {
expect(RawKeyboard.instance.keysPressed, contains(LogicalKeyboardKey.capsLock));
}, skip: isBrowser); // [intended] This is an Android-specific group.
testWidgets('Allows inconsistent modifier for Web - Alt graph', (WidgetTester _) async {
// Regression test for https://github.com/flutter/flutter/issues/113836
final List<RawKeyEvent> events = <RawKeyEvent>[];
RawKeyboard.instance.addListener(events.add);
addTearDown(() {
RawKeyboard.instance.removeListener(events.add);
});
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'web',
'code': 'AltRight',
'key': 'AltGraph',
'location': 2,
'metaState': 0,
'keyCode': 225,
}),
(ByteData? data) { },
);
expect(events, hasLength(1));
final RawKeyEvent altRightKey = events[0];
final RawKeyEventDataWeb data = altRightKey.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.altRight));
expect(data.logicalKey, equals(LogicalKeyboardKey.altGraph));
expect(RawKeyboard.instance.keysPressed, contains(LogicalKeyboardKey.altGraph));
}, skip: !isBrowser); // [intended] This is a Browser-specific test.
testWidgets('Allows inconsistent modifier for Web - Alt right', (WidgetTester _) async {
// Regression test for https://github.com/flutter/flutter/issues/113836
final List<RawKeyEvent> events = <RawKeyEvent>[];
RawKeyboard.instance.addListener(events.add);
addTearDown(() {
RawKeyboard.instance.removeListener(events.add);
});
await TestDefaultBinaryMessengerBinding.instance!.defaultBinaryMessenger.handlePlatformMessage(
SystemChannels.keyEvent.name,
SystemChannels.keyEvent.codec.encodeMessage(const <String, dynamic>{
'type': 'keydown',
'keymap': 'web',
'code': 'AltRight',
'key': 'Alt',
'location': 2,
'metaState': 0,
'keyCode': 225,
}),
(ByteData? data) { },
);
expect(events, hasLength(1));
final RawKeyEvent altRightKey = events[0];
final RawKeyEventDataWeb data = altRightKey.data as RawKeyEventDataWeb;
expect(data.physicalKey, equals(PhysicalKeyboardKey.altRight));
expect(data.logicalKey, equals(LogicalKeyboardKey.altRight));
expect(RawKeyboard.instance.keysPressed, contains(LogicalKeyboardKey.altRight));
}, skip: !isBrowser); // [intended] This is a Browser-specific test.
testWidgets('Dispatch events to all handlers', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
final List<int> logs = <int>[];