Skip to content

Commit eccd612

Browse files
JIWON1923loic-sharma
authored andcommitted
Fix Space input after Alt+Space with previous Microsoft IME
1 parent f5cd205 commit eccd612

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)