Fix issue where web embedder is synthesizing key up events too eagerly#180692
Conversation
There was a problem hiding this comment.
Code Review
This pull request fixes an issue where key up events were synthesized too eagerly by the web embedder. The change restricts the key guard mechanism, which synthesizes these events, to only be active when a modifier key is pressed. This prevents incorrect key up events for normal typing. The implementation is sound and includes a regression test to verify the fix. I have a minor suggestion to improve the clarity of the new test case.
engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart
Outdated
Show resolved
Hide resolved
| /// the user has released the key and we synthesize a keyup event. A limitation | ||
| /// in this approach is that if multiple non-modifier keys are pressed at the same time, | ||
| /// only the last pressed non-modifier key will send repeated events. This means that | ||
| /// the guard will eventually synthesize keyup events for the other keys, even if they | ||
| /// are still pressed. |
There was a problem hiding this comment.
The same issue fixed in this PR is also present in key combos with one or more modifier keys and multiple non-modifier keys (eg. cmd+i+j). In practice this means eg.
- Press and hold cmd
- Press and hold a
- Press and hold b
- Wait for over 2 seconds (>_kKeydownCancelDurationMac)
Expected events: cmd down, a down, b down
Actual events: cmd down, a down, b down, a up (synthesized)
This is a bit shame but at least I could not think of ways to work around this, any ideas are welcome. One option would be to delay the synthesizing of those non-modifier keys until the modifier is released (so the debouncing could be fully removed). But not sure if that would be any better as with that option too the framework might be out of sync for the non-modifier keys until the modifier is released.
There was a problem hiding this comment.
Here's an idea:
Right now, we have a key guard for each physical, and we track repeat events for each physical key individually like you said. What if we make it a single key guard for all of the pressed keys. If any of the pressed keys is repeated, we keep restarting the single key guard. Do you think this could work?
There was a problem hiding this comment.
That could work and would be slightly better than my original suggestion in this thread. In your suggestion releasing all other keys than the modifier key will synthesize the release of the non-modifier keys, whereas in my suggestion they'd be stuck as pressed until modifier is released. 🤔
However, both of the approaches have some drawbacks:
- The real state of keyboard might be out of sync with the state in the framework. In my suggestion all non-modifier keys could be released and the framework would think they're pressed until modifier is released. In your suggestion all but one non-modifier keys could be released and the framework would think they're pressed until the final non-modifier is released.
- The real release order is lost. We have no idea in which order the keys were actually released, so the synthesized events might (and most probably will) be fired in the wrong order. Not sure if we can do anything about this drawback as we're limited by the browsers events, which are pretty bad to begin with when modifier key is involved.
Should I open another issue for this sub-issue when the modifier keys are involved? I think we could still go forward with this PR as it fixes the issue perfectly when no modifier is involved (might already offer the necessary improvement for many game-devs). Whatever the solution for modifier key-combos will be, it will be a workaround. The browser already sends the key events perfectly when no modifier keys are involved, so we should handle those separately from the workaround (as we do in this PR).
There was a problem hiding this comment.
In your suggestion all but one non-modifier keys could be released and the framework would think they're pressed until the final non-modifier is released.
Is this true?
Assume keys "R", "S" and "T" are pressed. The browser keeps sending repeat events for one of them, say "T".
When "R", for example, is released, the browser sends a "keyup" event, the engine will translate that to a ui.KeyEventType.up, the framework will know that "R" is no longer pressed.
I'm not sure why you say "the framework would think they're pressed until the final non-modifier is released". Could you clarify please?
There was a problem hiding this comment.
Assume keys "R", "S" and "T" are pressed. The browser keeps sending repeat events for one of them, say "T".
When "R", for example, is released, the browser sends a "keyup" event, the engine will translate that to a ui.KeyEventType.up, the framework will know that "R" is no longer pressed.
Yes, this is true when no modifier key is involved. But its not true if one or more modifier is pressed.
How I got to this conclusion?
- Print the key up and key down dom events
- Press these keys in this order in the sample app
- cmd down
- j down
- k down
- j up
- k up
- cmd up
- See the logs, only key up event logged is the one for cmd
With your suggested solution (if I understood it correctly) releasing the last non-modifier key will synthesize the key up event for all non-modifier keys. This means that as long as one or more non-modifier key is pressed, no key-up events will be synthesized (even if some keys are released) -> thus "the framework would think they're pressed until the final non-modifier is released".
There was a problem hiding this comment.
Oh wow, that's weird. Looks like the behavior with Meta is different from other modifier keys. When Meta is pressed, the browser doesn't send keyup events for other keys.
We should make sure that the case of Meta + one key is handled correctly.
Other cases that involve Meta + multiple keys are impossible to handle 100% correctly. Any solution we pick is going to have downsides, and I don't have a strong opinion on which solution we should go with. I'll accept your PR as it's simpler than what I suggested earlier.
There was a problem hiding this comment.
Implementation improvements
Oh, did some more debugging and that seems to be right, its only meta that behaves differently, not all modifiers. The changes in this PR were done under the assumption that its all modifiers. I'll modify the implementation to match with the results of this debugging. Good catch 🎣, don't know how I ended up thinking its all modifiers 😅
The meta limitations
/// A limitation in this approach is that if meta key + multiple non-modifier keys are
/// pressed at the same time, only the last pressed non-modifier key will send repeated
/// events. This means that the guard will eventually synthesize keyup events for the other
/// keys, even if they are still pressed.Just to be clear, the changes in this PR don't yet do anything discussed in this thread for the meta + 2 or more non-modifier keys. The changes simply skip the key-up event debouncing if meta is not pressed, since in that case we can trust the events browser is sending. So with the changes in this PR, the same issue reported in the linked issue and documented in the comment in code still exist if meta-key is involved. So this will still happen, even though it probably should not:
- cmd down
- j down
- k down
- wait for 1500ms
- j up is synthesized, even though its still pressed
Should I try to fix the meta-key too in this PR, or open a separate issue for it? I think separate issue might be good, since its impossible to handle 100% correctly -> might be worth asking around opinions on which of the ways to handle it is the least bad. The workaround for the meta-case anyways has to be kept separate from the changes in this PR (the non-meta case) as that we can handle 100% correctly by trusting the browser events.
There was a problem hiding this comment.
I think this PR is good as is. It fixes the general issue with multiple key presses.
Pressing Meta + multiple keys while holding and releasing randomly is not a common pattern. Usually Meta is used for activating shortcuts, which typically involve pressing Meta + key 1 + key 2 then quickly releasing everything.
The issue of holding multiple keys and releasing some of them and then pressing others, sounds like a gaming thing. I imagine those keys usually don't include Meta.
I say let's merge this PR as is, and create a new issue for the Meta case with detailed explanation from this thread. We can fix it separately later. What do you think?
There was a problem hiding this comment.
Sounds good, I'll open another issue! Thanks for the review 🎉
…eb' of github.com:O-Hannonen/flutter into 162305-keyboard-callbacks-and-listener-not-working-in-web
|
Last issue causing the failure in |
Roll Flutter from 1141b2bdce66 to 46fb7210422d (38 revisions) flutter/flutter@1141b2b...46fb721 2026-03-02 [email protected] [rules] Fix a few issues in the full-length rules file (flutter/flutter#182725) 2026-03-02 [email protected] Roll Dart SDK from 8063f5f5ac60 to e86dbe9aa742 (1 revision) (flutter/flutter#183120) 2026-03-02 [email protected] [web] Roll Chrome to 145 (flutter/flutter#182860) 2026-03-02 [email protected] Roll Skia from 61c0e71760f5 to e180358b7a7a (1 revision) (flutter/flutter#183118) 2026-03-02 [email protected] Roll Packages from a27d7c5 to faa4e22 (4 revisions) (flutter/flutter#183117) 2026-03-02 [email protected] Add information to issue triage page (flutter/flutter#182145) 2026-03-02 [email protected] Roll Skia from cc8ce92481f2 to 61c0e71760f5 (2 revisions) (flutter/flutter#183103) 2026-03-02 [email protected] Roll Skia from 4cf3cd27b620 to cc8ce92481f2 (1 revision) (flutter/flutter#183100) 2026-03-02 [email protected] Roll Fuchsia Linux SDK from zN2ZV9QD0LD8acUFF... to 0dCDM2oORHwDf_pyb... (flutter/flutter#183101) 2026-03-01 [email protected] Update fl_texture_gl.h (flutter/flutter#182999) 2026-03-01 [email protected] Roll Skia from be1362b5ca4e to 4cf3cd27b620 (1 revision) (flutter/flutter#183096) 2026-03-01 [email protected] Roll Skia from b9210eb7005f to be1362b5ca4e (1 revision) (flutter/flutter#183092) 2026-03-01 [email protected] Timeout when waiting for the correct sized frame from Flutter. (flutter/flutter#182971) 2026-03-01 [email protected] Roll Skia from 28172a4e03af to b9210eb7005f (1 revision) (flutter/flutter#183088) 2026-03-01 [email protected] Roll Fuchsia Linux SDK from D7IYacJUCpvc_1iU_... to zN2ZV9QD0LD8acUFF... (flutter/flutter#183076) 2026-02-28 [email protected] Roll Dart SDK from cdf45eaf995e to 8063f5f5ac60 (1 revision) (flutter/flutter#183064) 2026-02-28 [email protected] Roll Dart SDK from 54451fcdbcf9 to cdf45eaf995e (1 revision) (flutter/flutter#183057) 2026-02-28 [email protected] Roll Skia from c8bcc27f5319 to 28172a4e03af (3 revisions) (flutter/flutter#183056) 2026-02-28 [email protected] Roll Dart SDK from 148d91b8a603 to 54451fcdbcf9 (2 revisions) (flutter/flutter#183051) 2026-02-28 [email protected] [A11y] in calendar date picker, remove SemanticsService.sendAnnouncement usage for android. (flutter/flutter#182918) 2026-02-28 [email protected] Add desktop review teams (flutter/flutter#182972) 2026-02-28 [email protected] [framework] Fix Text.semanticsIdentifier being absorbed by ancestor nodes (flutter/flutter#181795) 2026-02-28 [email protected] Roll Skia from b150186d3e23 to c8bcc27f5319 (5 revisions) (flutter/flutter#183032) 2026-02-28 [email protected] [Impeller] For Android hardware buffers on Vulkan, use an alpha value of 1 if the buffer format always has opaque alpha (flutter/flutter#182974) 2026-02-27 [email protected] Adds float32 output to `Image.toByteData()` in float32 Image (flutter/flutter#182847) 2026-02-27 [email protected] Roll Dart SDK from 1cdb7dfd913e to 148d91b8a603 (1 revision) (flutter/flutter#183025) 2026-02-27 [email protected] Roll Fuchsia Linux SDK from G1GwOdVt5bM7GjMSY... to D7IYacJUCpvc_1iU_... (flutter/flutter#183021) 2026-02-27 [email protected] When impellerc fails with a long shader compilation error, truncate it and output to a file (flutter/flutter#182786) 2026-02-27 [email protected] Add missing mutation-safe delegate iteration and use idomatic syntax (flutter/flutter#183018) 2026-02-27 [email protected] Exclude arm64 if any dependencies do and print warning when using Xcode 26 (flutter/flutter#182913) 2026-02-27 [email protected] Ignore unawaited_futures lint in dev/automated_tests (flutter/flutter#182922) 2026-02-27 [email protected] licenses_cpp: pre-land changes for perfetto update (flutter/flutter#182965) 2026-02-27 [email protected] Re-add extended attribute removed by SwiftPM (flutter/flutter#183011) 2026-02-27 [email protected] Fixes future warning for `await`ing `Future` returns in `async` bodies inside `try` blocks (flutter/flutter#182301) 2026-02-27 [email protected] Roll Skia from ed220c490eea to b150186d3e23 (2 revisions) (flutter/flutter#183014) 2026-02-27 [email protected] Fix issue where web embedder is synthesizing key up events too eagerly (flutter/flutter#180692) 2026-02-27 [email protected] chore: Don't unconditionally check tools/gn formatting (flutter/flutter#182973) 2026-02-27 [email protected] Roll Packages from e1d0169 to a27d7c5 (8 revisions) (flutter/flutter#183009) If this roll has caused a breakage, revert this CL and stop the roller using the controls here: https://autoroll.skia.org/r/flutter-packages Please CC [email protected],[email protected] on the revert to ensure that a human is aware of the problem. To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose ...

This PR fixes #162305
The reason this issue was visible in

Focus.onKeyEventbut not in the legacyFocus.onKeyis that the focus manager silently ignores the problematic synthesized messages forFocusNode.onKeyas they don't contain a raw event:Pre-launch Checklist
///).