[Win32, Keyboard] Process VK_PACKET; Unprintable keys block text (flutter/engine#31379)
* Tests * KeyStateChange * Format * InjectKeyboardChanges * Migrate KeyStateChange * Revert comment changes * protected queue * Remove assert * Move InjectKeyboardChanges to window * Rename to kKeyStateChange * Split MockMessageQueue messages. Remove WMsg.hWnd Fix embedder test Implement and finish tests * Format * Better doc * Impl * KeyUp overwrite_prev_state_0 * Tests * Comment * Fix compile * Format * Remove unnecessary changes * Remove outdated comment * Add redispatching count * Format
This commit is contained in:
@@ -227,19 +227,17 @@ void KeyboardManagerWin32::HandleOnKeyResult(
|
||||
is_syskey ||
|
||||
IsKeyDownShiftRight(event->key, event->was_down);
|
||||
|
||||
// For handled events, that's all.
|
||||
if (real_handled) {
|
||||
if (pending_text != pending_texts_.end()) {
|
||||
if (pending_text != pending_texts_.end()) {
|
||||
if (pending_text->placeholder || real_handled) {
|
||||
pending_texts_.erase(pending_text);
|
||||
} else {
|
||||
pending_text->ready = true;
|
||||
}
|
||||
return;
|
||||
DispatchReadyTexts();
|
||||
}
|
||||
|
||||
// For unhandled events, dispatch them to OnText.
|
||||
|
||||
if (pending_text != pending_texts_.end()) {
|
||||
pending_text->ready = true;
|
||||
DispatchReadyTexts();
|
||||
if (real_handled) {
|
||||
return;
|
||||
}
|
||||
|
||||
RedispatchEvent(std::move(event));
|
||||
@@ -327,10 +325,12 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
//
|
||||
// Checking `character` filters out non-printable event characters.
|
||||
std::list<PendingText>::iterator pending_text;
|
||||
if (action == WM_CHAR && character != 0) {
|
||||
if (action == WM_CHAR) {
|
||||
bool valid = character != 0 && IsPrintable(wparam);
|
||||
pending_texts_.push_back(PendingText{
|
||||
.ready = false,
|
||||
.content = text,
|
||||
.placeholder = !valid,
|
||||
});
|
||||
pending_text = std::prev(pending_texts_.end());
|
||||
} else {
|
||||
@@ -358,7 +358,7 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
// Also filter out ASCII control characters, which are sent as WM_CHAR
|
||||
// events for all control key shortcuts.
|
||||
current_session_.clear();
|
||||
if (action == WM_CHAR && IsPrintable(wparam)) {
|
||||
if (action == WM_CHAR) {
|
||||
pending_texts_.push_back(PendingText{
|
||||
.ready = true,
|
||||
.content = text,
|
||||
@@ -372,6 +372,9 @@ bool KeyboardManagerWin32::HandleMessage(UINT const action,
|
||||
case WM_SYSKEYDOWN:
|
||||
case WM_KEYUP:
|
||||
case WM_SYSKEYUP: {
|
||||
if (wparam == VK_PACKET) {
|
||||
return false;
|
||||
}
|
||||
current_session_.clear();
|
||||
current_session_.push_back(
|
||||
Win32Message{.action = action, .wparam = wparam, .lparam = lparam});
|
||||
|
||||
@@ -138,6 +138,7 @@ class KeyboardManagerWin32 {
|
||||
struct PendingText {
|
||||
bool ready;
|
||||
std::u16string content;
|
||||
bool placeholder = false;
|
||||
};
|
||||
|
||||
// Returns true if it's a new event, or false if it's a redispatched event.
|
||||
|
||||
@@ -2175,6 +2175,45 @@ TEST(KeyboardTest, TextInputSubmit) {
|
||||
//
|
||||
// Regression test for https://github.com/flutter/flutter/issues/97706.
|
||||
|
||||
// Press A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{'a', kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
|
||||
kLogicalKeyA, "a", kNotSynthesized);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[1], u"a");
|
||||
clear_key_calls();
|
||||
|
||||
// Release A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyUpInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
}
|
||||
|
||||
TEST(KeyboardTest, VietnameseTelexAddDiacriticWithFastResponse) {
|
||||
// In this test, the user presses the folloing keys:
|
||||
//
|
||||
// Key Current text
|
||||
// ===========================
|
||||
// A a
|
||||
// F à
|
||||
//
|
||||
// And the Backspace event is responded immediately.
|
||||
|
||||
KeyboardTester tester;
|
||||
tester.Responding(false);
|
||||
|
||||
// US Keyboard layout
|
||||
|
||||
// Press A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
@@ -2199,6 +2238,159 @@ TEST(KeyboardTest, TextInputSubmit) {
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 1);
|
||||
|
||||
// Press F, which is translated to:
|
||||
//
|
||||
// Backspace down, char & up, then VK_PACKET('à').
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{VK_BACK, kScanCodeBackspace, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{0x8, kScanCodeBackspace, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmKeyUpInfo{VK_BACK, kScanCodeBackspace, kNotExtended}.Build(
|
||||
kWmResultZero),
|
||||
WmKeyDownInfo{VK_PACKET, 0, kNotExtended, kWasUp}.Build(kWmResultDefault),
|
||||
WmCharInfo{0xe0 /*'à'*/, 0, kNotExtended, kWasUp}.Build(kWmResultZero),
|
||||
WmKeyUpInfo{VK_PACKET, 0, kNotExtended}.Build(kWmResultDefault)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 3);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown,
|
||||
kPhysicalBackspace, kLogicalBackspace, "",
|
||||
kNotSynthesized);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[1], kFlutterKeyEventTypeUp, kPhysicalBackspace,
|
||||
kLogicalBackspace, "", kNotSynthesized);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[2], u"à");
|
||||
clear_key_calls();
|
||||
// TODO(dkwingsmt): This count should probably be 4. Currently the CHAR 0x8
|
||||
// message is redispatched due to being part of the KeyDown session, which is
|
||||
// not handled by the framework, while the 'à' message is not redispatched
|
||||
// for being a standalone message. We should resolve this inconsistency.
|
||||
// https://github.com/flutter/flutter/issues/98306
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 3);
|
||||
|
||||
// Release F
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyUpInfo{kVirtualKeyF, kScanCodeKeyF, kNotExtended,
|
||||
/* overwrite_prev_state_0 */ true}
|
||||
.Build(kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, 0, 0, "",
|
||||
kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
}
|
||||
|
||||
void VietnameseTelexAddDiacriticWithSlowResponse(bool backspace_response) {
|
||||
// In this test, the user presses the folloing keys:
|
||||
//
|
||||
// Key Current text
|
||||
// ===========================
|
||||
// A a
|
||||
// F à
|
||||
//
|
||||
// And the Backspace down event is responded slowly with `backspace_response`.
|
||||
|
||||
KeyboardTester tester;
|
||||
tester.Responding(false);
|
||||
|
||||
// US Keyboard layout
|
||||
|
||||
// Press A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{'a', kScanCodeKeyA, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, kPhysicalKeyA,
|
||||
kLogicalKeyA, "a", kNotSynthesized);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[1], u"a");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 2);
|
||||
|
||||
// Release A
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyUpInfo{kVirtualKeyA, kScanCodeKeyA, kNotExtended}.Build(
|
||||
kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeUp, kPhysicalKeyA,
|
||||
kLogicalKeyA, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 1);
|
||||
|
||||
std::vector<MockKeyResponseController::ResponseCallback> recorded_callbacks;
|
||||
tester.LateResponding(
|
||||
[&recorded_callbacks](
|
||||
const FlutterKeyEvent* event,
|
||||
MockKeyResponseController::ResponseCallback callback) {
|
||||
recorded_callbacks.push_back(callback);
|
||||
});
|
||||
|
||||
// Press F, which is translated to:
|
||||
//
|
||||
// Backspace down, char & up, VK_PACKET('à').
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyDownInfo{VK_BACK, kScanCodeBackspace, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmCharInfo{0x8, kScanCodeBackspace, kNotExtended, kWasUp}.Build(
|
||||
kWmResultZero),
|
||||
WmKeyUpInfo{VK_BACK, kScanCodeBackspace, kNotExtended}.Build(
|
||||
kWmResultZero),
|
||||
WmKeyDownInfo{VK_PACKET, 0, kNotExtended, kWasUp}.Build(kWmResultDefault),
|
||||
WmCharInfo{0xe0 /*'à'*/, 0, kNotExtended, kWasUp}.Build(kWmResultZero),
|
||||
WmKeyUpInfo{VK_PACKET, 0, kNotExtended}.Build(kWmResultDefault)});
|
||||
|
||||
// The Backspace event has not responded yet, therefore the char message must
|
||||
// hold. This is because when the framework is handling the Backspace event,
|
||||
// it will send a setEditingState message that updates the text state that has
|
||||
// the last character deleted (denoted by `string1`). Processing the char
|
||||
// message before then will cause the final text to set to `string1`.
|
||||
EXPECT_EQ(key_calls.size(), 2);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown,
|
||||
kPhysicalBackspace, kLogicalBackspace, "",
|
||||
kNotSynthesized);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[1], kFlutterKeyEventTypeUp, kPhysicalBackspace,
|
||||
kLogicalBackspace, "", kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
|
||||
EXPECT_EQ(recorded_callbacks.size(), 2);
|
||||
recorded_callbacks[0](backspace_response);
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_TEXT(key_calls[0], u"à");
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(),
|
||||
backspace_response ? 0 : 2);
|
||||
|
||||
recorded_callbacks[1](false);
|
||||
EXPECT_EQ(key_calls.size(), 0);
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 1);
|
||||
|
||||
tester.Responding(false);
|
||||
|
||||
// Release F
|
||||
tester.InjectKeyboardChanges(std::vector<KeyboardChange>{
|
||||
WmKeyUpInfo{kVirtualKeyF, kScanCodeKeyF, kNotExtended,
|
||||
/* overwrite_prev_state_0 */ true}
|
||||
.Build(kWmResultZero)});
|
||||
|
||||
EXPECT_EQ(key_calls.size(), 1);
|
||||
EXPECT_CALL_IS_EVENT(key_calls[0], kFlutterKeyEventTypeDown, 0, 0, "",
|
||||
kNotSynthesized);
|
||||
clear_key_calls();
|
||||
EXPECT_EQ(tester.RedispatchedMessageCountAndClear(), 0);
|
||||
}
|
||||
|
||||
TEST(KeyboardTest, VietnameseTelexAddDiacriticWithSlowFalseResponse) {
|
||||
VietnameseTelexAddDiacriticWithSlowResponse(false);
|
||||
}
|
||||
|
||||
TEST(KeyboardTest, VietnameseTelexAddDiacriticWithSlowTrueResponse) {
|
||||
VietnameseTelexAddDiacriticWithSlowResponse(true);
|
||||
}
|
||||
|
||||
} // namespace testing
|
||||
|
||||
@@ -20,7 +20,7 @@ Win32Message WmKeyDownInfo::Build(LRESULT expected_result) {
|
||||
|
||||
Win32Message WmKeyUpInfo::Build(LRESULT expected_result) {
|
||||
uint32_t lParam = (1 /* repeat_count */ << 0) | (scan_code << 16) |
|
||||
(extended << 24) | (1 /* prev_state */ << 30) |
|
||||
(extended << 24) | (!overwrite_prev_state_0 << 30) |
|
||||
(1 /* transition */ << 31);
|
||||
return Win32Message{
|
||||
.message = WM_KEYUP,
|
||||
|
||||
@@ -84,6 +84,11 @@ typedef struct WmKeyUpInfo {
|
||||
|
||||
// uint16_t repeat_count; // Always 1.
|
||||
|
||||
// Set this flag to enforce prev_state to be 0.
|
||||
//
|
||||
// This occurs in rare cases when the message is synthesized.
|
||||
bool overwrite_prev_state_0;
|
||||
|
||||
Win32Message Build(LRESULT expected_result = kWmResultDontCheck);
|
||||
} WmKeyUpInfo;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user