Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 40a06f5

Browse files
authored
[Win32, Keyboard] Correctly process modifier events in IME mode (#30919)
1 parent 2693bd1 commit 40a06f5

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

shell/platform/windows/keyboard_key_embedder_handler.cc

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ uint64_t KeyboardKeyEmbedderHandler::GetPhysicalKey(int scancode,
126126
uint64_t KeyboardKeyEmbedderHandler::GetLogicalKey(int key,
127127
bool extended,
128128
int scancode) {
129+
if (key == VK_PROCESSKEY) {
130+
return VK_PROCESSKEY;
131+
}
132+
129133
// Normally logical keys should only be derived from key codes, but since some
130134
// key codes are either 0 or ambiguous (multiple keys using the same key
131135
// code), these keys are resolved by scan codes.
@@ -220,6 +224,15 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
220224
}
221225
}
222226

227+
if (result_logical_key == VK_PROCESSKEY) {
228+
// VK_PROCESSKEY means that the key press is used by an IME. These key
229+
// presses are considered handled and not sent to Flutter. These events must
230+
// be filtered by result_logical_key because the key up event of such
231+
// presses uses the "original" logical key.
232+
callback(true);
233+
return;
234+
}
235+
223236
UpdateLastSeenCritialKey(key, physical_key, result_logical_key);
224237
// Synchronize the toggled states of critical keys (such as whether CapsLocks
225238
// is enabled). Toggled states can only be changed upon a down event, so if
@@ -248,15 +261,6 @@ void KeyboardKeyEmbedderHandler::KeyboardHookImpl(
248261
pressingRecords_.erase(record_iter);
249262
}
250263

251-
if (result_logical_key == VK_PROCESSKEY) {
252-
// VK_PROCESSKEY means that the key press is used by an IME. These key
253-
// presses are considered handled and not sent to Flutter. These events must
254-
// be filtered by result_logical_key because the key up event of such
255-
// presses uses the "original" logical key.
256-
callback(true);
257-
return;
258-
}
259-
260264
FlutterKeyEvent key_data{
261265
.struct_size = sizeof(FlutterKeyEvent),
262266
.timestamp = static_cast<double>(

shell/platform/windows/keyboard_win32_unittests.cc

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1545,7 +1545,7 @@ TEST(KeyboardTest, MultibyteCharacter) {
15451545
tester.Responding(false);
15461546

15471547
// Gothic Keyboard layout. (We need a layout that yields non-BMP characters
1548-
// without IME, which that is actually very rare.)
1548+
// without IME, which is actually very rare.)
15491549

15501550
// Press key W of a US keyboard, which should yield character '𐍅'.
15511551
tester.InjectMessages(
@@ -1617,6 +1617,33 @@ TEST(KeyboardTest, NeverRedispatchShiftRightKeyDown) {
16171617
EXPECT_EQ(key_calls.size(), 0);
16181618
}
16191619

1620+
// Pressing modifiers during IME events should work properly by not sending any
1621+
// events.
1622+
//
1623+
// Regression test for https://github.com/flutter/flutter/issues/95888 .
1624+
TEST(KeyboardTest, ImeModifierEventsAreIgnored) {
1625+
KeyboardTester tester;
1626+
tester.Responding(false);
1627+
1628+
// US Keyboard layout.
1629+
1630+
// To make the keyboard into IME mode, there should have been events like
1631+
// letter key down with VK_PROCESSKEY. Omit them in this test since they don't
1632+
// seem significant.
1633+
1634+
// Press CtrlRight in IME mode.
1635+
tester.SetKeyState(VK_RCONTROL, true, false);
1636+
tester.InjectMessages(
1637+
1,
1638+
WmKeyDownInfo{VK_PROCESSKEY, kScanCodeControl, kExtended, kWasUp}.Build(
1639+
kWmResultZero));
1640+
1641+
EXPECT_EQ(key_calls.size(), 1);
1642+
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, 0, 0, "",
1643+
kNotSynthesized);
1644+
clear_key_calls();
1645+
}
1646+
16201647
TEST(KeyboardTest, DisorderlyRespondedEvents) {
16211648
KeyboardTester tester;
16221649

0 commit comments

Comments
 (0)