-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Fix Windows Enter key after focus loss #178523
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
It looks like this pull request may not have tests. Please make sure to add tests or get an explicit test exemption before merging. If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix? Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. If you believe this PR qualifies for a test exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!). The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request fixes a bug on Windows where a key press after losing and regaining focus could be ignored. The fix correctly synthesizes a key-up event to reset the internal key state before processing the new key-down event. The logic is sound and is accompanied by a new unit test that verifies the corrected behavior. I have one suggestion to improve code maintainability.
engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc
Outdated
Show resolved
Hide resolved
3763cc6 to
62ffe65
Compare
62ffe65 to
199dbde
Compare
|
cc @mattkae |
mattkae
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A thought, let me know what you think!
| // Windows delivered a new down event without a matching up event. This | ||
| // happens if the window lost focus before it could receive the key up. | ||
| // Synthesize an up event so the framework releases the key before | ||
| // processing the incoming down. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for doing this work! This might even affect a bug that I'm dealing with in the alt tab switcher.
I have a question though: Would it be better to move this logic to when the window gains focus instead? in pseudocode, it would be:
if window_gaining_focus:
if had_key_down:
synthesize_matching_up_event()
My reasoning for this is that the current implementation has to wait for another down event to be clicked to synthesize the up event for the previous key, which may add an unnecessary delay and may be incorrect in some cases (e.g. if "alt" is held and you return back to the app before clicking another key, you may move your mouse with a modifier held. This might mean something for certain apps).
Just my two cents, let me know what you think!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the idea.
I considered doing it on focus, but there are a couple of reasons I kept it in the key path:
KeyboardKeyEmbedderHandler isn’t told about focus changes, so "release on focus" would need new plumbing from FlutterWindow/WindowsLifecycleManager into the handler. I wanted the minimal, targeted fix.
Synthesizing ups on focus can be wrong if the OS still considers the key held (e.g., you alt‑tab back while still holding Alt). We’d force-release a modifier that the user is actually still holding, and any subsequent real keyup would then look out of order.
By deferring to the next real key event, we only synthesize when Windows tells us a fresh down is happening without a corresponding up, which aligns with the actual input stream.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable 👍
My only worry with this implementation is that the enter key could still be considered held while we mouse over widgets after receiving focus. This might mean something to some apps.
At any rate, we can address that in the future, as this PR does fix the most common use case and doesn't make the case that I outlined above any worse.
mattkae
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fix looks good to me, thanks! I think it will solve my issues with #177822 as well, but I will have to see (next week probably)
| // Windows delivered a new down event without a matching up event. This | ||
| // happens if the window lost focus before it could receive the key up. | ||
| // Synthesize an up event so the framework releases the key before | ||
| // processing the incoming down. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understandable 👍
My only worry with this implementation is that the enter key could still be considered held while we mouse over widgets after receiving focus. This might mean something to some apps.
At any rate, we can address that in the future, as this PR does fix the most common use case and doesn't make the case that I outlined above any worse.
flutter/flutter@9f383e0...d438df3 2025-11-21 [email protected] [ Tool ] Use a separate output directory when the native hooks run the build system (flutter/flutter#178840) 2025-11-21 [email protected] Fix Windows Enter key after focus loss (flutter/flutter#178523) 2025-11-21 [email protected] Roll Dart SDK from 369795548c09 to c788b6a7aefd (1 revision) (flutter/flutter#178924) 2025-11-21 [email protected] Roll Skia from 51fd48dccfb8 to d4e9d2873bfd (4 revisions) (flutter/flutter#178912) 2025-11-21 [email protected] Add changelog for 3.38.2 (flutter/flutter#178796) 2025-11-21 [email protected] Roll Dart SDK from 003a42bad376 to 369795548c09 (2 revisions) (flutter/flutter#178899) 2025-11-21 [email protected] Roll Skia from b9eafe0fab0c to 51fd48dccfb8 (1 revision) (flutter/flutter#178897) 2025-11-21 [email protected] Roll Dart SDK from 0894b46fff76 to 003a42bad376 (1 revision) (flutter/flutter#178889) 2025-11-21 [email protected] Make sure that a TextSelectionToolbarTextButton doesn't crash in 0x0 … (flutter/flutter#178374) 2025-11-21 [email protected] Make sure that a CupertinoSpellCheckSuggestionsToolbar doesn't crash … (flutter/flutter#177978) 2025-11-21 [email protected] Make sure that a TabBar doesn't crash in 0x0 environment (flutter/flutter#178201) 2025-11-20 [email protected] Make sure that a ToggleButtons doesn't crash in 0x0 environment (flutter/flutter#178454) 2025-11-20 [email protected] Roll Skia from d7b961c5f305 to b9eafe0fab0c (6 revisions) (flutter/flutter#178888) 2025-11-20 [email protected] Add macrobenchmark perf test for drawing arcs (flutter/flutter#178690) 2025-11-20 [email protected] Roll Dart SDK from 5b21f8a7d5d3 to 0894b46fff76 (1 revision) (flutter/flutter#178881) 2025-11-20 [email protected] Roll Skia from 6284b4f09e14 to d7b961c5f305 (1 revision) (flutter/flutter#178867) 2025-11-20 [email protected] Roll Fuchsia Linux SDK from 0z3qxzY6CWb8iVxEf... to Y-cMdgKy3d6EnibWR... (flutter/flutter#178865) 2025-11-20 [email protected] Roll Packages from 8f72e4b to b1e2fb0 (6 revisions) (flutter/flutter#178868) 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 To report a problem with the AutoRoller itself, please file a bug: https://issues.skia.org/issues/new?component=1389291&template=1850622 Documentation for the AutoRoller is here: https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
Fix issue: flutter#169447 Problem: On Windows, pressing Enter after refocusing the app could drop the first keydown because the embedder still thought the key was pressed, causing a double-press requirement and HardwareKeyboard assertions. Fix: In KeyboardKeyEmbedderHandler, synthesize a key-up whenever Win32 reports a “fresh” down for a key still tracked as pressed, so Flutter sees a release before the new down. This keeps modifier/lock-key synchronization intact. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…er app (#179097) fixes [#177822](#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by #178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…the flutter app (#179097)" (#179136) <!-- start_original_pr_link --> Reverts: #179097 <!-- end_original_pr_link --> <!-- start_initiating_author --> Initiated by: jtmcdole <!-- end_initiating_author --> <!-- start_revert_reason --> Reason for reverting: the tree is closed, should not have been merged. <!-- end_revert_reason --> <!-- start_original_pr_author --> Original PR Author: mattkae <!-- end_original_pr_author --> <!-- start_reviewers --> Reviewed By: {loic-sharma} <!-- end_reviewers --> <!-- start_revert_body --> This change reverts the following previous change: fixes [#177822](#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by #178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- end_revert_body --> Co-authored-by: auto-submit[bot] <[email protected]>
Fix issue: flutter#169447 Problem: On Windows, pressing Enter after refocusing the app could drop the first keydown because the embedder still thought the key was pressed, causing a double-press requirement and HardwareKeyboard assertions. Fix: In KeyboardKeyEmbedderHandler, synthesize a key-up whenever Win32 reports a “fresh” down for a key still tracked as pressed, so Flutter sees a release before the new down. This keeps modifier/lock-key synchronization intact. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…er app (flutter#179097) fixes [flutter#177822](flutter#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by flutter#178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…the flutter app (flutter#179097)" (flutter#179136) <!-- start_original_pr_link --> Reverts: flutter#179097 <!-- end_original_pr_link --> <!-- start_initiating_author --> Initiated by: jtmcdole <!-- end_initiating_author --> <!-- start_revert_reason --> Reason for reverting: the tree is closed, should not have been merged. <!-- end_revert_reason --> <!-- start_original_pr_author --> Original PR Author: mattkae <!-- end_original_pr_author --> <!-- start_reviewers --> Reviewed By: {loic-sharma} <!-- end_reviewers --> <!-- start_revert_body --> This change reverts the following previous change: fixes [flutter#177822](flutter#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by flutter#178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- end_revert_body --> Co-authored-by: auto-submit[bot] <[email protected]>
Fix issue: flutter#169447 Problem: On Windows, pressing Enter after refocusing the app could drop the first keydown because the embedder still thought the key was pressed, causing a double-press requirement and HardwareKeyboard assertions. Fix: In KeyboardKeyEmbedderHandler, synthesize a key-up whenever Win32 reports a “fresh” down for a key still tracked as pressed, so Flutter sees a release before the new down. This keeps modifier/lock-key synchronization intact. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…er app (flutter#179097) fixes [flutter#177822](flutter#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by flutter#178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing.
…the flutter app (flutter#179097)" (flutter#179136) <!-- start_original_pr_link --> Reverts: flutter#179097 <!-- end_original_pr_link --> <!-- start_initiating_author --> Initiated by: jtmcdole <!-- end_initiating_author --> <!-- start_revert_reason --> Reason for reverting: the tree is closed, should not have been merged. <!-- end_revert_reason --> <!-- start_original_pr_author --> Original PR Author: mattkae <!-- end_original_pr_author --> <!-- start_reviewers --> Reviewed By: {loic-sharma} <!-- end_reviewers --> <!-- start_revert_body --> This change reverts the following previous change: fixes [flutter#177822](flutter#177822) ## What's new? - The `FlutterWindow` now consumes any `WM_SYSKEYDOWN (VK_MENU)` message - Wrote a test to validate that this is the case - Make it so that the `KeyboardManager` can be provided This PR was made possible by flutter#178523 ## How to test? 1. Create a new flutter project 2. Add the following: ```dart import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; void main() { runApp(const MyApp()); HardwareKeyboard.instance.addHandler((event) { if (event is KeyDownEvent) { print('Key pressed: ${event.logicalKey.debugName}'); } return false; }); } // Rest of file unchanged from flutter create output ``` 3. Click alt. Note that the message is received. 4. Continue clicking alt and validate that the message is received. 5. Alt tab out and back and note that subsequent alts are correctly received. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement]. - [x] I signed the [CLA]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] I followed the [breaking change policy] and added [Data Driven Fixes] where supported. - [x] All existing and new tests are passing. <!-- end_revert_body --> Co-authored-by: auto-submit[bot] <[email protected]>
Fix issue: #169447
Problem: On Windows, pressing Enter after refocusing the app could drop the first keydown because the embedder still thought the key was pressed, causing a double-press requirement and HardwareKeyboard assertions.
Fix: In KeyboardKeyEmbedderHandler, synthesize a key-up whenever Win32 reports a “fresh” down for a key still tracked as pressed, so Flutter sees a release before the new down. This keeps modifier/lock-key synchronization intact.
Pre-launch Checklist
///).