[Win32] Don't redispatch ShiftRight keydown event (flutter/engine#25882)

Makes Win32 stop redispatching ShiftRight keydown event even if the framework does not handle it.
This commit is contained in:
Tong Mu
2021-05-03 15:33:02 -07:00
committed by GitHub
parent e8afb83d19
commit 0184cd3c70
2 changed files with 57 additions and 2 deletions

View File

@@ -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,

View File

@@ -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<TestFlutterKeyEvent> results;
TestFlutterKeyEvent* event;
bool last_handled = false;
std::unique_ptr<KeyboardKeyEmbedderHandler> handler =
std::make_unique<KeyboardKeyEmbedderHandler>(
[&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