Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions lib/web_ui/lib/src/engine/keyboard.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,6 @@ class Keyboard {
}

final html.KeyboardEvent keyboardEvent = event;

if (_shouldPreventDefault(event)) {
event.preventDefault();
}

final String timerKey = keyboardEvent.code!;

// Don't handle synthesizing a keyup event for modifier keys
Expand Down Expand Up @@ -132,16 +127,17 @@ class Keyboard {
};

EnginePlatformDispatcher.instance.invokeOnPlatformMessage('flutter/keyevent',
_messageCodec.encodeMessage(eventData), _noopCallback);
}

bool _shouldPreventDefault(html.KeyboardEvent event) {
switch (event.key) {
case 'Tab':
return true;
default:
return false;
}
_messageCodec.encodeMessage(eventData), (ByteData? data) {
if (data == null) {
return;
}
final Map<String, dynamic> jsonResponse = _messageCodec.decodeMessage(data);
if (jsonResponse['handled'] as bool) {
// If the framework handled it, then don't propagate it any further.
event.preventDefault();
}
},
);
}

void _synthesizeKeyup(html.KeyboardEvent event) {
Expand Down
29 changes: 28 additions & 1 deletion lib/web_ui/test/keyboard_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -226,13 +226,15 @@ void testMain() {
expect(count, 2);
});

test('prevents default when "Tab" is pressed', () {
test('prevents default when key is handled by the framework', () {
Keyboard.initialize();

int count = 0;
ui.window.onPlatformMessage = (String channel, ByteData data,
ui.PlatformMessageResponseCallback callback) {
count += 1;
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
callback(response);
};

final html.KeyboardEvent event = dispatchKeyboardEvent(
Expand All @@ -247,6 +249,29 @@ void testMain() {
Keyboard.instance.dispose();
});

test("Doesn't prevent default when key is not handled by the framework", () {
Keyboard.initialize();

int count = 0;
ui.window.onPlatformMessage = (String channel, ByteData data,
ui.PlatformMessageResponseCallback callback) {
count += 1;
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': false});
callback(response);
};

final html.KeyboardEvent event = dispatchKeyboardEvent(
'keydown',
key: 'Tab',
code: 'Tab',
);

expect(event.defaultPrevented, isFalse);
expect(count, 1);

Keyboard.instance.dispose();
});

test('keyboard events should be triggered on text fields', () {
Keyboard.initialize();

Expand Down Expand Up @@ -278,6 +303,8 @@ void testMain() {
ui.window.onPlatformMessage = (String channel, ByteData data,
ui.PlatformMessageResponseCallback callback) {
count += 1;
ByteData response = const JSONMessageCodec().encodeMessage(<String, dynamic>{'handled': true});
callback(response);
};

useTextEditingElement((html.Element element) {
Expand Down