Skip to content

Commit 774b051

Browse files
authored
[Windows] Fix Space input after Alt+Space with previous Microsoft IME (flutter#192105)
<!-- Thanks for filing a pull request! Reviewers are typically assigned within a week of filing a request. To learn more about code review, see our documentation on Tree Hygiene: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md --> Fixes flutter#189495 ## Description After `Alt+Space` opens the Windows system menu, the Space key-up does not always reach Flutter while the native menu is active. Flutter can therefore retain a stale pressed record that maps the physical Space key to the logical Space key. With **Use previous version of Microsoft IME** enabled, the next physical Space press can first arrive as a fresh `WM_KEYDOWN(VK_PROCESSKEY)` with the Space scan code, followed by the translated `WM_KEYDOWN(VK_SPACE)` event. The existing stale-state recovery synthesizes the missing Space key-up, but then continues processing the `VK_PROCESSKEY` event and records it as the logical key for physical Space. The following translated `VK_SPACE` event consequently inherits `VK_PROCESSKEY` as its sequence logical key and is filtered as an IME event. Space input then remains unavailable. When this state is detected, the handler now synthesizes the missing key-up and filters the fresh `VK_PROCESSKEY` event without recording it. The following translated `VK_SPACE` event is then processed normally. The observed failing event is `WM_KEYDOWN(VK_PROCESSKEY)`. The implementation reuses the handler's existing definition of a key-down event, which also includes `WM_SYSKEYDOWN`, so the same pressed-state invariant is preserved for system key-down messages. The early path additionally requires an existing record for the same physical key and a previous-key-state bit indicating a fresh press. Normal IME events without a stale pressed record, key-up events, and repeated key-down events continue through the existing paths. Microsoft documents the previous IME setting here: [Revert to a previous version of an Input Method Editor (IME)](https://support.microsoft.com/en-us/windows/hardware/input-devices/revert-to-a-previous-version-of-an-input-method-editor-ime) ## Win32 behavior Microsoft documents that: - [`WM_SYSKEYDOWN`](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-syskeydown) is posted when Alt is held while another key is pressed. - Bit 30 of [`WM_KEYDOWN`](https://learn.microsoft.com/en-us/windows/win32/inputdev/wm-keydown) indicates the previous key state, allowing a fresh down transition to be distinguished from an auto-repeat. - An IME changes the virtual-key value of an already processed key message to `VK_PROCESSKEY`, as described by [`ImmGetVirtualKey`](https://learn.microsoft.com/en-us/windows/win32/api/imm/nf-imm-immgetvirtualkey). - [`SC_KEYMENU`](https://learn.microsoft.com/en-us/windows/win32/menurc/wm-syscommand) identifies a window-menu request caused by a keystroke. Here, `229` is `VK_PROCESSKEY`, `32` is `VK_SPACE`, and scan code `57` is the physical Space key. <details> <summary>Diagnostic log excerpts</summary> ### Current Microsoft IME The next Space press arrives directly as `VK_SPACE`. The existing stale-state recovery handles this case: ```text [flutter#189495][child-window] message=256 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 space_async_down=1 alt_async_down=0 [flutter#189495][child-window] message=258 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 space_async_down=1 alt_async_down=0 [flutter#189495][embedder:entry] action=256 key=32 scancode=57 character=32 extended=0 was_down=0 physical_key=458796 logical_key=32 had_record=1 last_logical_record=32 pressed_count=1 [flutter#189495][embedder:stale-record] physical_key=458796 last_logical_record=32 incoming_logical_key=32 ``` ### Previous Microsoft IME before this change A fresh `VK_PROCESSKEY` arrives first. The existing recovery replaces the logical record with `229`, so the translated `VK_SPACE` event is also filtered: ```text [flutter#189495][window] WM_KEYDOWN message=256 wparam=229 scan_code=57 extended=0 context=0 was_down=0 transition=0 space_async_down=1 alt_async_down=0 has_focus=1 is_active=0 is_foreground=0 [flutter#189495][embedder:entry] action=256 key=229 scancode=57 character=0 extended=0 was_down=0 physical_key=458796 logical_key=229 had_record=1 last_logical_record=32 pressed_count=1 [flutter#189495][embedder:stale-record] physical_key=458796 last_logical_record=32 incoming_logical_key=229 [flutter#189495][window] WM_KEYDOWN message=256 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 space_async_down=1 alt_async_down=0 has_focus=1 is_active=0 is_foreground=0 [flutter#189495][window] WM_CHAR message=258 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 space_async_down=1 alt_async_down=0 has_focus=1 is_active=0 is_foreground=0 [flutter#189495][embedder:entry] action=256 key=32 scancode=57 character=32 extended=0 was_down=0 physical_key=458796 logical_key=32 had_record=1 last_logical_record=229 pressed_count=1 [flutter#189495][embedder:ime-filter] action=256 key=32 sequence_logical_key=229 had_record=1 ``` ### Previous Microsoft IME after this change The stale Space record is released while filtering the fresh `VK_PROCESSKEY`. The translated `VK_SPACE` event then arrives with no stale record and is processed as a normal key-down: ```text [flutter#189495][window] message=256 wparam=229 scan_code=57 extended=0 context=0 was_down=0 transition=0 [flutter#189495][embedder:entry] action=256 key=229 scancode=57 character=0 extended=0 was_down=0 physical_key=458796 logical_key=229 had_record=1 last_logical_record=32 pressed_count=1 [flutter#189495][embedder:synthesize-up] physical_key=458796 logical_key=32 pressed_count_before=1 [flutter#189495][embedder:synthesize-up] pressed_count_after=0 [flutter#189495][window] message=256 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 [flutter#189495][window] message=258 wparam=32 scan_code=57 extended=0 context=0 was_down=0 transition=0 [flutter#189495][embedder:entry] action=256 key=32 scancode=57 character=32 extended=0 was_down=0 physical_key=458796 logical_key=32 had_record=0 last_logical_record=0 pressed_count=0 [flutter#189495][embedder:sequence] key=32 sequence_logical_key=32 ime_filter=0 [flutter#189495][embedder:decision] down result_logical_key=32 ``` </details> ## Screenshot https://github.com/user-attachments/assets/ae2a66c1-6536-442c-a430-c8fdb0dc1c6e ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [AI contribution guidelines] and understand my responsibilities, or I am not using AI tools. - [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 in-code documentation (doc comments with `///`). - [x] If this PR introduces a new feature or capability, I created and linked a website documentation issue or PR in [flutter/website] (or verified none is needed). - [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. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance. **Note**: The Flutter team is currently trialing the use of [Gemini Code Assist for GitHub](https://developers.google.com/gemini-code-assist/docs/review-github-code). Comments from the `gemini-code-assist` bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview [AI contribution guidelines]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#ai-contribution-guidelines [Tree Hygiene]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md [Features we expect every widget to implement]: https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [flutter/website]: https://github.com/flutter/website [breaking change policy]: https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md [Data Driven Fixes]: https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
1 parent 37a1588 commit 774b051

2 files changed

Lines changed: 76 additions & 2 deletions

File tree

‎engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,16 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
167167
uint64_t last_logical_record =
168168
had_record ? last_logical_record_iter->second : 0;
169169

170+
const bool is_event_down = action == WM_KEYDOWN || action == WM_SYSKEYDOWN;
171+
172+
if (is_event_down && key == VK_PROCESSKEY && had_record && !was_down) {
173+
// A native modal loop can leave a key recorded as pressed. Release that
174+
// stale state before filtering the IME's fresh key-down.
175+
SendSynthesizeUpEvent(physical_key, last_logical_record);
176+
callback(true);
177+
return;
178+
}
179+
170180
// The logical key for the current "tap sequence".
171181
//
172182
// Key events are formed in tap sequences: down, repeats, up. The logical key
@@ -183,8 +193,6 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
183193
return;
184194
}
185195

186-
const bool is_event_down = action == WM_KEYDOWN || action == WM_SYSKEYDOWN;
187-
188196
bool event_key_can_be_repeat = true;
189197
UpdateLastSeenCriticalKey(key, physical_key, sequence_logical_key);
190198
// Synchronize the toggled states of critical keys (such as whether CapsLocks

‎engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler_unittests.cc‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ UINT DefaultMapVkToScan(UINT virtual_key, bool extended) {
7575

7676
constexpr uint64_t kScanCodeKeyA = 0x1e;
7777
constexpr uint64_t kScanCodeAltLeft = 0x38;
78+
constexpr uint64_t kScanCodeSpace = 0x39;
7879
constexpr uint64_t kScanCodeNumpad1 = 0x4f;
7980
constexpr uint64_t kScanCodeNumLock = 0x45;
8081
constexpr uint64_t kScanCodeControl = 0x1d;
@@ -327,6 +328,71 @@ TEST(KeyboardKeyEmbedderHandlerTest, ImeEventsAreIgnored) {
327328
EXPECT_EQ(last_handled, true);
328329
}
329330

331+
// Press Alt+Space, miss Space's key-up, and press Space with an IME.
332+
//
333+
// This is special because the previous Microsoft IME reports the next Space as
334+
// VK_PROCESSKEY while Flutter still records the previous Space as pressed.
335+
TEST(KeyboardKeyEmbedderHandlerTest, FreshImeKeyDownReleasesStalePressedKey) {
336+
TestKeystate key_state;
337+
std::vector<TestFlutterKeyEvent> results;
338+
bool last_handled = false;
339+
340+
std::unique_ptr<KeyboardKeyEmbedderHandler> handler =
341+
std::make_unique<KeyboardKeyEmbedderHandler>(
342+
[&results](const FlutterKeyEvent& event,
343+
FlutterKeyEventCallback callback, void* user_data) {
344+
results.emplace_back(event, callback, user_data);
345+
},
346+
key_state.Getter(), DefaultMapVkToScan);
347+
348+
// Press Space while Alt is held. Omit its key-up.
349+
handler->KeyboardHook(VK_SPACE, kScanCodeSpace, WM_SYSKEYDOWN, 0, false,
350+
false, [](bool) {});
351+
ASSERT_EQ(results.size(), 1u);
352+
results[0].callback(false, results[0].user_data);
353+
results.clear();
354+
355+
// Press Space through the IME.
356+
handler->KeyboardHook(
357+
VK_PROCESSKEY, kScanCodeSpace, WM_KEYDOWN, 0, false, false,
358+
[&last_handled](bool handled) { last_handled = handled; });
359+
EXPECT_EQ(last_handled, true);
360+
ASSERT_EQ(results.size(), 1u);
361+
EXPECT_EQ(results[0].type, kFlutterKeyEventTypeUp);
362+
EXPECT_EQ(results[0].physical, kPhysicalSpace);
363+
EXPECT_EQ(results[0].logical, kLogicalSpace);
364+
EXPECT_EQ(results[0].synthesized, true);
365+
EXPECT_EQ(handler->GetPressedState().empty(), true);
366+
results.clear();
367+
368+
// Dispatch the translated Space key-down.
369+
handler->KeyboardHook(VK_SPACE, kScanCodeSpace, WM_KEYDOWN, ' ', false, false,
370+
[](bool) {});
371+
ASSERT_EQ(results.size(), 1u);
372+
EXPECT_EQ(results[0].type, kFlutterKeyEventTypeDown);
373+
EXPECT_EQ(results[0].physical, kPhysicalSpace);
374+
EXPECT_EQ(results[0].logical, kLogicalSpace);
375+
ASSERT_EQ(handler->GetPressedState().size(), 1u);
376+
EXPECT_EQ(handler->GetPressedState().at(kPhysicalSpace), kLogicalSpace);
377+
results[0].callback(false, results[0].user_data);
378+
results.clear();
379+
380+
// Repeat Space through the IME.
381+
last_handled = false;
382+
handler->KeyboardHook(
383+
VK_PROCESSKEY, kScanCodeSpace, WM_KEYDOWN, 0, false, true,
384+
[&last_handled](bool handled) { last_handled = handled; });
385+
EXPECT_EQ(last_handled, false);
386+
ASSERT_EQ(results.size(), 1u);
387+
EXPECT_EQ(results[0].type, kFlutterKeyEventTypeRepeat);
388+
EXPECT_EQ(results[0].logical, kLogicalSpace);
389+
EXPECT_EQ(results[0].synthesized, false);
390+
ASSERT_EQ(handler->GetPressedState().size(), 1u);
391+
EXPECT_EQ(handler->GetPressedState().at(kPhysicalSpace), kLogicalSpace);
392+
results[0].callback(false, results[0].user_data);
393+
EXPECT_EQ(last_handled, false);
394+
}
395+
330396
// Test if modifier keys that are told apart by the extended bit can be
331397
// identified. (Their physical keys must be searched with the extended bit
332398
// considered.)

0 commit comments

Comments
 (0)