Skip to content

Commit

Permalink
[Web] Update modifier state when Meta key is seen as Process key (#50779
Browse files Browse the repository at this point in the history
)

## Description

On Web, browsers can emit key events with a logical key sets to `Process` when the physical key is MetaLeft. Because the modifier state is 0 despite Meta key being pressed this will trigger an assert.
This PR adds some logic for this specific case. Maybe a more slightly broader solution will be needed (using the same logic for all modifiers ?). I focused on MetaLeft because it was directly reported on  flutter/flutter#141186.

## Related Issue

Fixes flutter/flutter#141186.

## Tests

Adds 1 test.
  • Loading branch information
bleroux authored Feb 21, 2024
1 parent 3c2c3a0 commit 9100d32
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/web_ui/lib/src/engine/raw_keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ class RawKeyboard {
} else if (event.key == 'Meta' && operatingSystem == OperatingSystem.linux) {
// On Chrome Linux, metaState can be wrong when a Meta key is pressed.
_lastMetaState |= _modifierMeta;
} else if (event.code == 'MetaLeft' && event.key == 'Process') {
// When Meta key is pressed, browsers can emit an event whose key is 'Process'.
// See https://github.com/flutter/flutter/issues/141186.
_lastMetaState |= _modifierMeta;
}
}
final Map<String, dynamic> eventData = <String, dynamic>{
Expand Down
34 changes: 33 additions & 1 deletion lib/web_ui/test/engine/raw_keyboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ void testMain() {
});

// Regression test for https://github.com/flutter/flutter/issues/125672.
test('updates meta state for Meta key and wrong DOM event metaKey value', () {
test('updates meta state for Meta key and wrong DOM event metaKey value (Linux)', () {
RawKeyboard.initialize();

Map<String, dynamic>? dataReceived;
Expand Down Expand Up @@ -181,6 +181,38 @@ void testMain() {
RawKeyboard.instance!.dispose();
}, skip: operatingSystem != OperatingSystem.linux);

// Regression test for https://github.com/flutter/flutter/issues/141186.
test('updates meta state for Meta key seen as "Process" key', () {
RawKeyboard.initialize();

Map<String, dynamic>? dataReceived;
ui.PlatformDispatcher.instance.onPlatformMessage = (String channel, ByteData? data,
ui.PlatformMessageResponseCallback? callback) {
dataReceived = const JSONMessageCodec().decodeMessage(data) as Map<String, dynamic>?;
};

// Purposely send a DOM event where Meta key is pressed but event.metaKey is not set to true.
// This can happen when the Meta key is seen as the 'Process' key.
final DomKeyboardEvent event = dispatchKeyboardEvent(
'keydown',
key: 'Process',
code: 'MetaLeft',
location: 1,
keyCode: 229,
);
expect(event.defaultPrevented, isFalse);
expect(dataReceived, <String, dynamic>{
'type': 'keydown',
'keymap': 'web',
'code': 'MetaLeft',
'key': 'Process',
'location': 1,
'metaState': 0x8,
'keyCode': 229,
});
RawKeyboard.instance!.dispose();
});

test('dispatches repeat events', () {
RawKeyboard.initialize();

Expand Down

0 comments on commit 9100d32

Please sign in to comment.