diff --git a/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc b/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc index 84ee0a100c..ae4fc807c6 100644 --- a/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc +++ b/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler.cc @@ -126,6 +126,23 @@ uint64_t KeyboardKeyEmbedderHandler::getLogicalKey(int key, } } +// Returns true if this key is a special key that Flutter must not redispatch. +// +// This is a temporary solution to +// https://github.com/flutter/flutter/issues/81674, and forces ShiftRight +// KeyDown event to not be redispatched regardless of the framework's response. +// +// If a ShiftRight KeyDown event is not handled by the framework and is +// redispatched, Win32 will not send its following KeyUp event and keeps +// recording ShiftRight as being pressed. +static bool IsEventThatMustNotRedispatch(int virtual_key, bool was_down) { +#ifdef WINUWP + return false; +#else + return virtual_key == VK_RSHIFT && !was_down; +#endif +} + void KeyboardKeyEmbedderHandler::KeyboardHook( int key, int scancode, @@ -228,17 +245,21 @@ void KeyboardKeyEmbedderHandler::KeyboardHook( .character = character_bytes, .synthesized = false, }; + const bool must_not_redispatch = IsEventThatMustNotRedispatch(key, was_down); response_id_ += 1; uint64_t response_id = response_id_; PendingResponse pending{ .callback = - [this, callback = std::move(callback)](bool handled, - uint64_t response_id) { + [this, callback = std::move(callback), must_not_redispatch]( + bool handled, uint64_t response_id) { auto found = pending_responses_.find(response_id); if (found != pending_responses_.end()) { pending_responses_.erase(found); } + if (must_not_redispatch) { + handled = true; + } callback(handled); }, .response_id = response_id, diff --git a/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler_unittests.cc b/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler_unittests.cc index e992d5d91c..1427dcc4d4 100644 --- a/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler_unittests.cc +++ b/engine/src/flutter/shell/platform/windows/keyboard_key_embedder_handler_unittests.cc @@ -837,5 +837,39 @@ TEST(KeyboardKeyEmbedderHandlerTest, SynthesizeWithInitialTogglingState) { results.clear(); } +// A key down event for shift right must not be redispatched even if +// the framework returns unhandled. +// +// The reason for this test is documented in |IsEventThatMustNotRedispatch|. +TEST(KeyboardKeyEmbedderHandlerTest, NeverRedispatchShiftRightKeyDown) { + TestKeystate key_state; + std::vector results; + TestFlutterKeyEvent* event; + bool last_handled = false; + + std::unique_ptr handler = + std::make_unique( + [&results](const FlutterKeyEvent& event, + FlutterKeyEventCallback callback, void* user_data) { + results.emplace_back(event, callback, user_data); + }, + key_state.Getter()); + + // Press ShiftRight. + key_state.Set(VK_RSHIFT, true); + handler->KeyboardHook( + VK_RSHIFT, kScanCodeShiftRight, WM_KEYDOWN, 0, false, false, + [&last_handled](bool handled) { last_handled = handled; }); + EXPECT_EQ(last_handled, false); + EXPECT_EQ(results.size(), 1); + + // Framework does not handle it + event = &results[0]; + event->callback(false, event->user_data); + // Still responds with handling the event + EXPECT_EQ(last_handled, true); + results.clear(); +} + } // namespace testing } // namespace flutter