Windows: Fix AltGr key causing CtrlLeft to hang (flutter/engine#27266)

Fixes an issue where pressing AltGr key (on supporting layouts such as Spanish) on Windows causes CtrlLeft to hang pressed for both Flutter and Windows.

Co-authored-by: Greg Spencer <gspencergoog@users.noreply.github.com>
This commit is contained in:
Tong Mu
2021-07-11 21:38:22 -07:00
committed by GitHub
parent 207c1f524a
commit 1bdcf5501f
8 changed files with 479 additions and 86 deletions

View File

@@ -19,7 +19,7 @@ REM Test if Git is available on the Host
where /q git || ECHO Error: Unable to find git in your PATH. && EXIT /B 1
SET repo_dir=%SRC_DIR%\flutter
SET ci_dir=%repo_dir%\flutter\ci
SET ci_dir=%repo_dir%\ci
SET dart_sdk_path=%SRC_DIR%\third_party\dart\tools\sdks\dart-sdk
SET dart=%dart_sdk_path%\bin\dart.exe

View File

@@ -52,9 +52,8 @@ struct SimulatedEvent {
// key event handler.
class SpyKeyboardKeyHandler : public KeyboardHandlerBase {
public:
SpyKeyboardKeyHandler(
flutter::BinaryMessenger* messenger,
KeyboardKeyHandler::EventRedispatcher redispatch_event) {
SpyKeyboardKeyHandler(flutter::BinaryMessenger* messenger,
KeyboardKeyHandler::EventDispatcher redispatch_event) {
real_implementation_ =
std::make_unique<KeyboardKeyHandler>(redispatch_event);
real_implementation_->AddDelegate(

View File

@@ -77,11 +77,11 @@ void FlutterWindowsView::RegisterKeyboardHandlers(
// handler is "toggled" when events pass through, therefore the redispatching
// algorithm does not allow more than 1 handler that takes |SendInput|.
#ifdef WINUWP
flutter::KeyboardKeyHandler::EventRedispatcher redispatch_event = nullptr;
flutter::KeyboardKeyHandler::EventDispatcher redispatch_event = nullptr;
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
nullptr;
#else
flutter::KeyboardKeyHandler::EventRedispatcher redispatch_event = SendInput;
flutter::KeyboardKeyHandler::EventDispatcher redispatch_event = SendInput;
flutter::KeyboardKeyEmbedderHandler::GetKeyStateHandler get_key_state =
GetKeyState;
#endif

View File

@@ -128,23 +128,6 @@ 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,
@@ -247,21 +230,17 @@ 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), must_not_redispatch](
bool handled, uint64_t response_id) {
[this, callback = std::move(callback)](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,39 +837,5 @@ 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

View File

@@ -18,13 +18,85 @@ namespace {
// emitting a warning on the console about unhandled events.
static constexpr int kMaxPendingEvents = 1000;
// Returns true if this key is a key down event of ShiftRight.
//
// This is a temporary solution to
// https://github.com/flutter/flutter/issues/81674, and forces ShiftRight
// KeyDown events 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 IsKeyDownShiftRight(int virtual_key, bool was_down) {
#ifdef WINUWP
return false;
#else
return virtual_key == VK_RSHIFT && !was_down;
#endif
}
// Returns true if this key is an AltRight key down event.
//
// This is used to resolve an issue where an AltGr press causes CtrlLeft to hang
// when pressed, as reported in https://github.com/flutter/flutter/issues/78005.
//
// When AltGr is pressed (in a supporting layout such as Spanish), Win32 first
// fires a fake CtrlLeft down event, then an AltRight down event.
// This is significant because this fake CtrlLeft down event will not be paired
// with a up event, which is fine until Flutter redispatches the CtrlDown
// event, which Win32 then interprets as a real event, leaving both Win32 and
// the Flutter framework thinking that CtrlLeft is still pressed.
//
// To resolve this, Flutter recognizes this fake CtrlLeft down event using the
// following AltRight down event. Flutter then synthesizes a CtrlLeft key up
// event immediately after the corresponding AltRight key up event.
//
// One catch is that it is impossible to distinguish the fake CtrlLeft down
// from a normal CtrlLeft down (followed by a AltRight down), since they
// contain the exactly same information, including the GetKeyState result.
// Fortunately, this will require the two events to occur *really* close, which
// would be rare, and a misrecognition would only cause a minor consequence
// where the CtrlLeft is released early; the later, real, CtrlLeft up event will
// be ignored.
static bool IsKeyDownAltRight(int action, int virtual_key, bool extended) {
#ifdef WINUWP
return false;
#else
return virtual_key == VK_LMENU && extended && action == WM_KEYDOWN;
#endif
}
// Returns true if this key is a key up event of AltRight.
//
// This is used to assist a corner case described in |IsKeyDownAltRight|.
static bool IsKeyUpAltRight(int action, int virtual_key, bool extended) {
#ifdef WINUWP
return false;
#else
return virtual_key == VK_LMENU && extended && action == WM_KEYUP;
#endif
}
// Returns true if this key is a key down event of CtrlLeft.
//
// This is used to assist a corner case described in |IsKeyDownAltRight|.
static bool IsKeyDownCtrlLeft(int action, int virtual_key) {
#ifdef WINUWP
return false;
#else
return virtual_key == VK_LCONTROL && action == WM_KEYDOWN;
#endif
}
} // namespace
KeyboardKeyHandler::KeyboardKeyHandlerDelegate::~KeyboardKeyHandlerDelegate() =
default;
KeyboardKeyHandler::KeyboardKeyHandler(EventRedispatcher redispatch_event)
: redispatch_event_(redispatch_event), last_sequence_id_(1) {}
KeyboardKeyHandler::KeyboardKeyHandler(EventDispatcher dispatch_event)
: dispatch_event_(dispatch_event),
last_sequence_id_(1),
last_key_is_ctrl_left_down(false) {}
KeyboardKeyHandler::~KeyboardKeyHandler() = default;
@@ -40,40 +112,50 @@ size_t KeyboardKeyHandler::RedispatchedCount() {
return pending_redispatches_.size();
}
void KeyboardKeyHandler::RedispatchEvent(std::unique_ptr<PendingEvent> event) {
// TODO(dkwingsmt) consider adding support for redispatching events for UWP
void KeyboardKeyHandler::DispatchEvent(const PendingEvent& event) {
// TODO(dkwingsmt) consider adding support for dispatching events for UWP
// in order to support add-to-app.
// https://github.com/flutter/flutter/issues/70202
#ifdef WINUWP
return;
#else
uint8_t scancode = event->scancode;
char32_t character = event->character;
char32_t character = event.character;
INPUT input_event{
.type = INPUT_KEYBOARD,
.ki =
KEYBDINPUT{
.wVk = 0,
.wScan = static_cast<WORD>(event->scancode),
.wVk = static_cast<WORD>(event.key),
.wScan = static_cast<WORD>(event.scancode),
.dwFlags = static_cast<WORD>(
KEYEVENTF_SCANCODE |
(event->extended ? KEYEVENTF_EXTENDEDKEY : 0x0) |
(event->action == WM_KEYUP ? KEYEVENTF_KEYUP : 0x0)),
(event.extended ? KEYEVENTF_EXTENDEDKEY : 0x0) |
(event.action == WM_KEYUP ? KEYEVENTF_KEYUP : 0x0)),
},
};
pending_redispatches_.push_back(std::move(event));
UINT accepted = redispatch_event_(1, &input_event, sizeof(input_event));
UINT accepted = dispatch_event_(1, &input_event, sizeof(input_event));
if (accepted != 1) {
std::cerr << "Unable to synthesize event for unhandled keyboard event "
"with scancode "
<< scancode << " (character " << character << ")" << std::endl;
std::cerr << "Unable to synthesize event for keyboard event with scancode "
<< event.scancode;
if (character != 0) {
std::cerr << " (character " << character << ")";
}
std::cerr << std::endl;
;
}
#endif
}
void KeyboardKeyHandler::RedispatchEvent(std::unique_ptr<PendingEvent> event) {
#ifdef WINUWP
return;
#else
DispatchEvent(*event);
pending_redispatches_.push_back(std::move(event));
#endif
}
bool KeyboardKeyHandler::KeyboardHook(FlutterWindowsView* view,
int key,
int scancode,
@@ -96,10 +178,39 @@ bool KeyboardKeyHandler::KeyboardHook(FlutterWindowsView* view,
return false;
}
if (IsKeyDownAltRight(action, key, extended)) {
if (last_key_is_ctrl_left_down) {
should_synthesize_ctrl_left_up = true;
}
}
if (IsKeyDownCtrlLeft(action, key)) {
last_key_is_ctrl_left_down = true;
ctrl_left_scancode = scancode;
should_synthesize_ctrl_left_up = false;
} else {
last_key_is_ctrl_left_down = false;
}
if (IsKeyUpAltRight(action, key, extended)) {
if (should_synthesize_ctrl_left_up) {
should_synthesize_ctrl_left_up = false;
PendingEvent ctrl_left_up{
.key = VK_LCONTROL,
.scancode = ctrl_left_scancode,
.action = WM_KEYUP,
.was_down = true,
};
DispatchEvent(ctrl_left_up);
}
}
uint64_t sequence_id = ++last_sequence_id_;
incoming->sequence_id = sequence_id;
incoming->unreplied = delegates_.size();
incoming->any_handled = false;
// There are a few situations where events must not be redispatched.
// Initializing `any_handled` with true in such cases suffices, since it can
// only be set to true and is used to disable redispatching.
const bool must_not_redispatch = IsKeyDownShiftRight(key, was_down);
incoming->any_handled = must_not_redispatch;
if (pending_responds_.size() > kMaxPendingEvents) {
std::cerr
@@ -148,7 +259,7 @@ void KeyboardKeyHandler::ResolvePendingEvent(uint64_t sequence_id,
assert(event.unreplied >= 0);
// If all delegates have replied, redispatch if no one handled.
if (event.unreplied == 0) {
auto event_ptr = std::move(*iter);
std::unique_ptr<PendingEvent> event_ptr = std::move(*iter);
pending_responds_.erase(iter);
if (!event_ptr->any_handled) {
RedispatchEvent(std::move(event_ptr));

View File

@@ -53,14 +53,14 @@ class KeyboardKeyHandler : public KeyboardHandlerBase {
virtual ~KeyboardKeyHandlerDelegate();
};
using EventRedispatcher =
using EventDispatcher =
std::function<UINT(UINT cInputs, LPINPUT pInputs, int cbSize)>;
// Create a KeyboardKeyHandler and specify where to redispatch events.
//
// The |redispatch_event| is typically |SendInput|, but can also be nullptr
// (for UWP).
explicit KeyboardKeyHandler(EventRedispatcher redispatch_event);
explicit KeyboardKeyHandler(EventDispatcher dispatch_event);
~KeyboardKeyHandler();
@@ -140,6 +140,8 @@ class KeyboardKeyHandler : public KeyboardHandlerBase {
uint64_t hash;
};
void DispatchEvent(const PendingEvent& event);
// Find an event in the redispatch list that matches the given one.
//
// If an matching event is found, removes the matching event from the
@@ -161,8 +163,23 @@ class KeyboardKeyHandler : public KeyboardHandlerBase {
// The sequence_id attached to the last event sent to the framework.
uint64_t last_sequence_id_;
// The callback used to redispatch synthesized events.
EventRedispatcher redispatch_event_;
// The callback used to dispatch synthesized events.
EventDispatcher dispatch_event_;
// Whether the last event is a CtrlLeft key down.
//
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
bool last_key_is_ctrl_left_down;
// The scancode of the last met CtrlLeft down.
//
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
uint8_t ctrl_left_scancode;
// Whether a CtrlLeft up should be synthesized upon the next AltRight up.
//
// This is used to resolve a corner case described in |IsKeyDownAltRight|.
bool should_synthesize_ctrl_left_up;
// Calculate a hash based on event data for fast comparison for a redispatched
// event.

View File

@@ -18,6 +18,10 @@ static constexpr int kHandledScanCode = 20;
static constexpr int kHandledScanCode2 = 22;
static constexpr int kUnhandledScanCode = 21;
constexpr uint64_t kScanCodeShiftRight = 0x36;
constexpr uint64_t kScanCodeControlLeft = 0x1D;
constexpr uint64_t kScanCodeAltLeft = 0x38;
typedef std::function<void(bool)> Callback;
typedef std::function<void(Callback&)> CallbackHandler;
void dont_respond(Callback& callback) {}
@@ -90,7 +94,7 @@ class MockKeyHandlerDelegate
class TestKeyboardKeyHandler : public KeyboardKeyHandler {
public:
explicit TestKeyboardKeyHandler(EventRedispatcher redispatch_event)
explicit TestKeyboardKeyHandler(EventDispatcher redispatch_event)
: KeyboardKeyHandler(redispatch_event) {}
bool HasRedispatched() { return RedispatchedCount() > 0; }
@@ -407,5 +411,322 @@ TEST(KeyboardKeyHandlerTest, WithSlowFrameworkResponse) {
hook_history.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 |IsKeyDownShiftRight|.
TEST(KeyboardKeyHandlerTest, NeverRedispatchShiftRightKeyDown) {
std::list<MockKeyHandlerDelegate::KeyboardHookCall> hook_history;
// Capture the scancode of the last redispatched event
int redispatch_scancode = 0;
bool delegate_handled = false;
TestKeyboardKeyHandler handler([&redispatch_scancode](UINT cInputs,
LPINPUT pInputs,
int cbSize) -> UINT {
EXPECT_TRUE(cbSize > 0);
redispatch_scancode = pInputs->ki.wScan;
return 1;
});
auto delegate = std::make_unique<MockKeyHandlerDelegate>(1, &hook_history);
delegate->callback_handler = respond_false;
handler.AddDelegate(std::move(delegate));
// Press ShiftRight and the delegate responds false.
delegate_handled = handler.KeyboardHook(
nullptr, VK_RSHIFT, kScanCodeShiftRight, WM_KEYDOWN, 0, false, false);
EXPECT_EQ(delegate_handled, true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
}
TEST(KeyboardKeyHandlerTest, AltGr) {
std::list<MockKeyHandlerDelegate::KeyboardHookCall> hook_history;
// Capture the scancode of the last redispatched event
int redispatch_scancode = 0;
TestKeyboardKeyHandler handler([&redispatch_scancode](UINT cInputs,
LPINPUT pInputs,
int cbSize) -> UINT {
EXPECT_TRUE(cbSize > 0);
redispatch_scancode = pInputs->ki.wScan;
return 1;
});
auto delegate = std::make_unique<MockKeyHandlerDelegate>(1, &hook_history);
delegate->callback_handler = dont_respond;
handler.AddDelegate(std::move(delegate));
// Sequence 1: Tap AltGr.
// The key down event causes a ControlLeft down and a AltRight (extended
// AltLeft) down.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
true);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 2);
EXPECT_EQ(hook_history.front().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.front().was_down, false);
EXPECT_EQ(hook_history.back().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.back().was_down, false);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
false);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
// The key up event only causes a AltRight (extended AltLeft) up.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
true);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.front().was_down, true);
// A ControlLeft key up is synthesized.
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
true);
EXPECT_EQ(hook_history.back().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.back().was_down, true);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
false);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
// Sequence 2: Tap CtrlLeft and AltGr.
// This also tests tapping AltGr twice in a row when combined with sequence
// 1 since "tapping CtrlLeft and AltGr" only sends an extra CtrlLeft key up
// than "tapping AltGr".
// Key down ControlLeft.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.front().was_down, false);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
hook_history.clear();
redispatch_scancode = 0;
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
false);
// Key down AltRight.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.front().was_down, false);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
hook_history.clear();
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
false);
redispatch_scancode = 0;
// Key up AltRight.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
true);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.front().was_down, true);
// A ControlLeft key up is synthesized.
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
true);
EXPECT_EQ(hook_history.back().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.back().was_down, true);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
false);
hook_history.clear();
redispatch_scancode = 0;
// Key up ControlLeft should be dispatched to delegates, but will be properly
// handled by delegates' logic.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.front().was_down, true);
hook_history.front().callback(true);
EXPECT_EQ(redispatch_scancode, 0);
hook_history.clear();
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
// Sequence 3: Hold AltGr for repeated events.
// Every AltGr key repeat event is also preceded by a ControlLeft down
// (repeat).
// Key down AltRight.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
true);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 2);
EXPECT_EQ(hook_history.front().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.front().was_down, false);
EXPECT_EQ(hook_history.back().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.back().was_down, false);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
false);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
// Another key down AltRight.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, true),
true);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, true),
true);
EXPECT_EQ(redispatch_scancode, 0);
EXPECT_EQ(hook_history.size(), 2);
EXPECT_EQ(hook_history.front().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.front().was_down, true);
EXPECT_EQ(hook_history.back().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.back().was_down, true);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYDOWN, 0, false, false),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft,
WM_KEYDOWN, 0, true, false),
false);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
// Key up AltRight.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
true);
EXPECT_EQ(hook_history.size(), 1);
EXPECT_EQ(hook_history.front().scancode, kScanCodeAltLeft);
EXPECT_EQ(hook_history.front().was_down, true);
// A ControlLeft key up is synthesized.
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
true);
EXPECT_EQ(hook_history.back().scancode, kScanCodeControlLeft);
EXPECT_EQ(hook_history.back().was_down, true);
hook_history.front().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeAltLeft);
hook_history.back().callback(false);
EXPECT_EQ(redispatch_scancode, kScanCodeControlLeft);
// Resolve redispatches.
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LCONTROL, kScanCodeControlLeft,
WM_KEYUP, 0, false, true),
false);
EXPECT_EQ(handler.KeyboardHook(nullptr, VK_LMENU, kScanCodeAltLeft, WM_KEYUP,
0, true, true),
false);
EXPECT_EQ(handler.HasRedispatched(), false);
redispatch_scancode = 0;
hook_history.clear();
}
} // namespace testing
} // namespace flutter