Skip to content

Commit

Permalink
Relax the event assumptions for keydown events. (flutter#53830)
Browse files Browse the repository at this point in the history
This PR addresses an issue where autocompleting a text field, even without direct keyboard input, unexpectedly triggers keydown events. To resolve this, the code now relaxes the casting assumptions to accommodate a wider range of event types, not just keyboard events.

By just adding the following script to the console, and filling the text field using autocomplete, you can see that indeed the fired event is not of type `KeyboardEvent` but `Event`. 

```javascript
document.body.addEventListener('keydown', console.log)
```

Fixes flutter/flutter#149968

*If you had to change anything in the [flutter/tests] repo, include a link to the migration guide as per the [breaking change policy].*

[C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
  • Loading branch information
tugorez authored Jul 12, 2024
1 parent c006670 commit 63dd507
Showing 1 changed file with 4 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ final class ViewFocusBinding {
});

late final DomEventListener _handleKeyDown = createDomEventListener((DomEvent event) {
event as DomKeyboardEvent;
if (event.shiftKey ?? false) {
// The right event type needs to be checked because Chrome seems to be firing
// `Event` events instead of `KeyboardEvent` events when autofilling is used.
// See https://github.com/flutter/flutter/issues/149968 for more info.
if (event is DomKeyboardEvent && (event.shiftKey ?? false)) {
_viewFocusDirection = ui.ViewFocusDirection.backward;
}
});
Expand Down

0 comments on commit 63dd507

Please sign in to comment.