Unverified Commit 4771605c authored by Tong Mu's avatar Tong Mu Committed by GitHub

Fix KeyboardManager's synthesization (#88967)

This PR fixes KeyboardManager's key event synthesization logic, which were dispatching events with incorrect keys, making subsequent key events crash the app.
parent 163b711c
......@@ -881,7 +881,7 @@ class KeyEventManager {
for (final PhysicalKeyboardKey key in physicalKeysPressed.difference(_rawKeyboard.physicalKeysPressed)) {
_keyEventsSinceLastMessage.add(KeyUpEvent(
physicalKey: key,
logicalKey: _hardwareKeyboard.lookUpLayout(physicalKey)!,
logicalKey: _hardwareKeyboard.lookUpLayout(key)!,
timeStamp: timeStamp,
synthesized: true,
));
......@@ -889,7 +889,7 @@ class KeyEventManager {
for (final PhysicalKeyboardKey key in _rawKeyboard.physicalKeysPressed.difference(physicalKeysPressed)) {
_keyEventsSinceLastMessage.add(KeyDownEvent(
physicalKey: key,
logicalKey: _rawKeyboard.lookUpLayout(physicalKey)!,
logicalKey: _rawKeyboard.lookUpLayout(key)!,
timeStamp: timeStamp,
synthesized: true,
));
......
......@@ -4,6 +4,7 @@
import 'dart:ui' as ui;
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_test/flutter_test.dart';
......@@ -67,6 +68,34 @@ void main() {
equals(<KeyboardLockMode>{}));
}, variant: KeySimulatorTransitModeVariant.keyDataThenRawKeyData());
testWidgets('KeyboardManager synthesizes modifier keys in rawKeyData mode', (WidgetTester tester) async {
final List<KeyEvent> events = <KeyEvent>[];
HardwareKeyboard.instance.addHandler((KeyEvent event) {
events.add(event);
return false;
});
// While ShiftLeft is held (the event of which was skipped), press keyA.
final Map<String, dynamic> rawMessage = kIsWeb ? (
KeyEventSimulator.getKeyData(
LogicalKeyboardKey.keyA,
platform: 'web',
)..['metaState'] = RawKeyEventDataWeb.modifierShift
) : (
KeyEventSimulator.getKeyData(
LogicalKeyboardKey.keyA,
platform: 'android',
)..['metaState'] = RawKeyEventDataAndroid.modifierLeftShift | RawKeyEventDataAndroid.modifierShift
);
tester.binding.keyEventManager.handleRawKeyMessage(rawMessage);
expect(events, hasLength(2));
expect(events[0].physicalKey, PhysicalKeyboardKey.shiftLeft);
expect(events[0].logicalKey, LogicalKeyboardKey.shiftLeft);
expect(events[0].synthesized, true);
expect(events[1].physicalKey, PhysicalKeyboardKey.keyA);
expect(events[1].logicalKey, LogicalKeyboardKey.keyA);
expect(events[1].synthesized, false);
});
testWidgets('Dispatch events to all handlers', (WidgetTester tester) async {
final FocusNode focusNode = FocusNode();
final List<int> logs = <int>[];
......
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