[Keyboard] Send empty key events when no key data should (flutter/engine#27774)

The keyboard system on each platform now sends an empty key data instead of nothing if no key data should be sent.
This commit is contained in:
Tong Mu
2021-07-30 13:35:39 -07:00
committed by GitHub
parent 6ce81e9d94
commit b2924176cc
13 changed files with 342 additions and 44 deletions

View File

@@ -76,6 +76,15 @@ const int _kDeadKeyShift = 0x20000000;
const int _kDeadKeyAlt = 0x40000000;
const int _kDeadKeyMeta = 0x80000000;
const ui.KeyData _emptyKeyData = ui.KeyData(
type: ui.KeyEventType.down,
timeStamp: Duration.zero,
logical: 0,
physical: 0,
character: null,
synthesized: false,
);
typedef DispatchKeyData = bool Function(ui.KeyData data);
/// Converts a floating number timestamp (in milliseconds) to a [Duration] by
@@ -401,6 +410,8 @@ class KeyboardConverter {
// a currently pressed one, usually indicating multiple keyboards are
// pressing keys with the same physical key, or the up event was lost
// during a loss of focus. The down event is ignored.
dispatchKeyData(_emptyKeyData);
event.preventDefault();
return;
}
} else {
@@ -413,6 +424,8 @@ class KeyboardConverter {
if (lastLogicalRecord == null) {
// The physical key has been released before. It indicates multiple
// keyboards pressed keys with the same physical key. Ignore the up event.
dispatchKeyData(_emptyKeyData);
event.preventDefault();
return;
}

View File

@@ -369,9 +369,12 @@ void testMain() {
converter.handleEvent(keyDownEvent('ShiftLeft', 'Shift', kShift, kLocationLeft)
..onPreventDefault = onPreventDefault
);
expect(keyDataList, isEmpty);
expect(preventedDefault, isFalse);
expect(keyDataList, hasLength(1));
expect(keyDataList[0].physical, 0);
expect(keyDataList[0].logical, 0);
expect(preventedDefault, isTrue);
keyDataList.clear();
converter.handleEvent(keyUpEvent('ShiftLeft', 'Shift', 0, kLocationLeft)
..onPreventDefault = onPreventDefault
);
@@ -398,8 +401,10 @@ void testMain() {
converter.handleEvent(keyUpEvent('ShiftRight', 'Shift', 0, kLocationRight)
..onPreventDefault = onPreventDefault
);
expect(keyDataList, isEmpty);
expect(preventedDefault, isFalse);
expect(keyDataList, hasLength(1));
expect(keyDataList[0].physical, 0);
expect(keyDataList[0].logical, 0);
expect(preventedDefault, isTrue);
});
test('Conflict from multiple keyboards do not crash', () {

View File

@@ -426,6 +426,15 @@ void HandleResponse(bool handled, void* user_data);
- (void)sendPrimaryFlutterEvent:(const FlutterKeyEvent&)event
callback:(nonnull FlutterKeyCallbackGuard*)callback;
/**
* Send an empty key event.
*
* The event is never synthesized, and never expects an event result. An empty
* event is sent when no other events should be sent, such as upon back-to-back
* keydown events of the same key.
*/
- (void)sendEmptyEvent;
/**
* Send a key event for a modifier key.
*/
@@ -619,6 +628,19 @@ void HandleResponse(bool handled, void* user_data);
_sendEvent(event, HandleResponse, pending);
}
- (void)sendEmptyEvent {
FlutterKeyEvent event = {
.struct_size = sizeof(FlutterKeyEvent),
.timestamp = 0,
.type = kFlutterKeyEventTypeDown,
.physical = 0,
.logical = 0,
.character = nil,
.synthesized = false,
};
_sendEvent(event, nil, nil);
}
- (void)synthesizeModifierEventOfType:(BOOL)isDownEvent
timestamp:(NSTimeInterval)timestamp
keyCode:(UInt32)keyCode {
@@ -659,6 +681,7 @@ void HandleResponse(bool handled, void* user_data);
// However this might happen in add-to-app scenarios if the focus is changed
// from the native view to the Flutter view amid the key tap.
[callback resolveTo:TRUE];
[self sendEmptyEvent];
return;
}
}
@@ -695,6 +718,7 @@ void HandleResponse(bool handled, void* user_data);
// However this might happen in add-to-app scenarios if the focus is changed
// from the native view to the Flutter view amid the key tap.
[callback resolveTo:TRUE];
[self sendEmptyEvent];
return;
}
[self updateKey:physicalKey asPressed:0];

View File

@@ -301,9 +301,16 @@ typedef void (^ResponseCallback)(bool handled);
last_handled = handled;
}];
XCTAssertEqual([events count], 0u);
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
XCTAssertEqual(event->physical, 0ull);
XCTAssertEqual(event->logical, 0ull);
XCTAssertEqual(event->synthesized, false);
XCTAssertFalse([[events lastObject] hasCallback]);
XCTAssertEqual(last_handled, TRUE);
[events removeAllObjects];
last_handled = FALSE;
[responder handlePress:keyUpEvent(kKeyCodeKeyA, kModifierFlagNone, 123.0f)
callback:^(BOOL handled) {
@@ -327,6 +334,7 @@ typedef void (^ResponseCallback)(bool handled);
- (void)testIgnoreAbruptUpEvent API_AVAILABLE(ios(13.4)) {
__block NSMutableArray<TestKeyEvent*>* events = [[NSMutableArray<TestKeyEvent*> alloc] init];
__block BOOL last_handled = TRUE;
FlutterKeyEvent* event;
FlutterEmbedderKeyResponder* responder = [[FlutterEmbedderKeyResponder alloc]
initWithSendEvent:^(const FlutterKeyEvent& event, _Nullable FlutterKeyEventCallback callback,
@@ -342,8 +350,15 @@ typedef void (^ResponseCallback)(bool handled);
last_handled = handled;
}];
XCTAssertEqual([events count], 0u);
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
XCTAssertEqual(event->physical, 0ull);
XCTAssertEqual(event->logical, 0ull);
XCTAssertEqual(event->synthesized, false);
XCTAssertFalse([[events lastObject] hasCallback]);
XCTAssertEqual(last_handled, TRUE);
[events removeAllObjects];
}
// Press R-Shift, A, then release R-Shift then A, on a US keyboard.
@@ -433,6 +448,10 @@ typedef void (^ResponseCallback)(bool handled);
- (void)testSpecialModiferFlags API_AVAILABLE(ios(13.4)) {
__block NSMutableArray<TestKeyEvent*>* events = [[NSMutableArray<TestKeyEvent*> alloc] init];
FlutterKeyEvent* event;
__block BOOL last_handled = TRUE;
id keyEventCallback = ^(BOOL handled) {
last_handled = handled;
};
FlutterEmbedderKeyResponder* responder = [[FlutterEmbedderKeyResponder alloc]
initWithSendEvent:^(const FlutterKeyEvent& event, _Nullable FlutterKeyEventCallback callback,
@@ -448,8 +467,7 @@ typedef void (^ResponseCallback)(bool handled);
// Numpad 1
// OS provides: char: "1", code: 0x59, modifiers: 0x200000
[responder handlePress:keyDownEvent(kKeyCodeNumpad1, kModifierFlagNumPadKey, 123.0, "1", "1")
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -465,8 +483,7 @@ typedef void (^ResponseCallback)(bool handled);
// Fn Key (sends HID undefined)
// OS provides: char: nil, keycode: 0x3, modifiers: 0x0
[responder handlePress:keyDownEvent(kKeyCodeUndefined, kModifierFlagNone, 123.0)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -482,8 +499,7 @@ typedef void (^ResponseCallback)(bool handled);
// F1 Down
// OS provides: char: UIKeyInputF1, code: 0x3a, modifiers: 0x0
[responder handlePress:keyDownEvent(kKeyCodeF1, kModifierFlagNone, 123.0f, "\\^P", "\\^P")
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -499,8 +515,7 @@ typedef void (^ResponseCallback)(bool handled);
// KeyA Down
// OS provides: char: "q", code: 0x4, modifiers: 0x0
[responder handlePress:keyDownEvent(kKeyCodeKeyA, kModifierFlagNone, 123.0f, "a", "a")
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -516,8 +531,7 @@ typedef void (^ResponseCallback)(bool handled);
// ShiftLeft Down
// OS Provides: char: nil, code: 0xe1, modifiers: 0x20000
[responder handlePress:keyDownEvent(kKeyCodeShiftLeft, kModifierFlagShiftAny, 123.0f)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -532,8 +546,7 @@ typedef void (^ResponseCallback)(bool handled);
// Numpad 1 Up
// OS provides: char: "1", code: 0x59, modifiers: 0x200000
[responder handlePress:keyUpEvent(kKeyCodeNumpad1, kModifierFlagNumPadKey, 123.0f)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 2u);
@@ -559,8 +572,7 @@ typedef void (^ResponseCallback)(bool handled);
// F1 Up
// OS provides: char: UIKeyInputF1, code: 0x3a, modifiers: 0x0
[responder handlePress:keyUpEvent(kKeyCodeF1, kModifierFlagNone, 123.0f)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -576,8 +588,7 @@ typedef void (^ResponseCallback)(bool handled);
// Fn Key (sends HID undefined)
// OS provides: char: nil, code: 0x3, modifiers: 0x0
[responder handlePress:keyUpEvent(kKeyCodeUndefined, kModifierFlagNone, 123.0)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -592,8 +603,7 @@ typedef void (^ResponseCallback)(bool handled);
// KeyA Up
// OS provides: char: "a", code: 0x4, modifiers: 0x0
[responder handlePress:keyUpEvent(kKeyCodeKeyA, kModifierFlagNone, 123.0f)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
@@ -609,10 +619,17 @@ typedef void (^ResponseCallback)(bool handled);
// ShiftLeft Up
// OS provides: char: nil, code: 0xe1, modifiers: 0x20000
[responder handlePress:keyUpEvent(kKeyCodeShiftLeft, kModifierFlagShiftAny, 123.0f)
callback:^(BOOL handled){
}];
callback:keyEventCallback];
XCTAssertEqual([events count], 0u);
XCTAssertEqual([events count], 1u);
event = [events lastObject].data;
XCTAssertEqual(event->physical, 0ull);
XCTAssertEqual(event->logical, 0ull);
XCTAssertEqual(event->synthesized, false);
XCTAssertFalse([[events lastObject] hasCallback]);
XCTAssertEqual(last_handled, TRUE);
[events removeAllObjects];
}
- (void)testIdentifyLeftAndRightModifiers API_AVAILABLE(ios(13.4)) {

View File

@@ -54,7 +54,9 @@ class PointerDataPacket {};
- (void)sendKeyEvent:(const FlutterKeyEvent&)event
callback:(FlutterKeyEventCallback)callback
userData:(void*)userData API_AVAILABLE(ios(9.0)) {
NSAssert(callback != nullptr, @"Invalid callback");
if (callback == nil)
return;
// NSAssert(callback != nullptr, @"Invalid callback");
// Response is async, so we have to post it to the run loop instead of calling
// it directly.
CFRunLoopPerformBlock(CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, ^() {

View File

@@ -423,6 +423,15 @@ const char* getEventString(NSString* characters) {
keyCode:(unsigned short)keyCode
callback:(nullable FlutterKeyCallbackGuard*)callback;
/**
* Send an empty key event.
*
* The event is never synthesized, and never expects an event result. An empty
* event is sent when no other events should be sent, such as upon back-to-back
* keydown events of the same key.
*/
- (void)sendEmptyEvent;
/**
* Processes a down event from the system.
*/
@@ -579,6 +588,7 @@ const char* getEventString(NSString* characters) {
uint64_t logicalKey = GetLogicalKeyForModifier(keyCode, physicalKey);
if (physicalKey == 0 || logicalKey == 0) {
NSLog(@"Unrecognized modifier key: keyCode 0x%hx, physical key 0x%llx", keyCode, physicalKey);
[self sendEmptyEvent];
[callback resolveTo:TRUE];
return;
}
@@ -599,6 +609,19 @@ const char* getEventString(NSString* characters) {
}
}
- (void)sendEmptyEvent {
FlutterKeyEvent flutterEvent = {
.struct_size = sizeof(FlutterKeyEvent),
.timestamp = 0,
.type = kFlutterKeyEventTypeDown,
.physical = 0,
.logical = 0,
.character = nil,
.synthesized = false,
};
_sendEvent(flutterEvent, nullptr, nullptr);
}
- (void)handleDownEvent:(NSEvent*)event callback:(FlutterKeyCallbackGuard*)callback {
uint64_t physicalKey = GetPhysicalKeyForKeyCode(event.keyCode);
uint64_t logicalKey = GetLogicalKeyForEvent(event, physicalKey);
@@ -611,6 +634,7 @@ const char* getEventString(NSString* characters) {
// key up event to the window where the corresponding key down occurred.
// However this might happen in add-to-app scenarios if the focus is changed
// from the native view to the Flutter view amid the key tap.
[self sendEmptyEvent];
[callback resolveTo:TRUE];
return;
}
@@ -643,6 +667,7 @@ const char* getEventString(NSString* characters) {
// key up event to the window where the corresponding key down occurred.
// However this might happen in add-to-app scenarios if the focus is changed
// from the native view to the Flutter view amid the key tap.
[self sendEmptyEvent];
[callback resolveTo:TRUE];
return;
}
@@ -669,6 +694,7 @@ const char* getEventString(NSString* characters) {
[self sendCapsLockTapWithTimestamp:event.timestamp callback:callback];
_lastModifierFlagsOfInterest = _lastModifierFlagsOfInterest ^ NSEventModifierFlagCapsLock;
} else {
[self sendEmptyEvent];
[callback resolveTo:TRUE];
}
}

View File

@@ -302,8 +302,15 @@ TEST(FlutterEmbedderKeyResponderUnittests, IgnoreDuplicateDownEvent) {
last_handled = handled;
}];
EXPECT_EQ([events count], 0u);
EXPECT_EQ([events count], 1u);
EXPECT_EQ(last_handled, TRUE);
event = [events lastObject].data;
EXPECT_EQ(event->physical, 0ull);
EXPECT_EQ(event->logical, 0ull);
EXPECT_FALSE([[events lastObject] hasCallback]);
EXPECT_EQ(last_handled, TRUE);
[events removeAllObjects];
last_handled = FALSE;
[responder handleEvent:keyEvent(NSEventTypeKeyUp, 0x100, @"a", @"a", FALSE, kKeyCodeKeyA)
@@ -327,6 +334,7 @@ TEST(FlutterEmbedderKeyResponderUnittests, IgnoreDuplicateDownEvent) {
TEST(FlutterEmbedderKeyResponderUnittests, IgnoreDuplicateUpEvent) {
__block NSMutableArray<TestKeyEvent*>* events = [[NSMutableArray<TestKeyEvent*> alloc] init];
FlutterKeyEvent* event;
__block BOOL last_handled = TRUE;
FlutterEmbedderKeyResponder* responder = [[FlutterEmbedderKeyResponder alloc]
@@ -343,8 +351,15 @@ TEST(FlutterEmbedderKeyResponderUnittests, IgnoreDuplicateUpEvent) {
last_handled = handled;
}];
EXPECT_EQ([events count], 0u);
EXPECT_EQ([events count], 1u);
EXPECT_EQ(last_handled, TRUE);
event = [events lastObject].data;
EXPECT_EQ(event->physical, 0ull);
EXPECT_EQ(event->logical, 0ull);
EXPECT_FALSE([[events lastObject] hasCallback]);
EXPECT_EQ(last_handled, TRUE);
[events removeAllObjects];
}
// Press L shift, A, then release L shift then A, on an US keyboard.
@@ -1042,6 +1057,7 @@ TEST(FlutterEmbedderKeyResponderUnittests, ConvertCapsLockEvents) {
// Press the CapsLock key when CapsLock state is desynchronized
TEST(FlutterEmbedderKeyResponderUnittests, SynchronizeCapsLockStateOnCapsLock) {
__block NSMutableArray<TestKeyEvent*>* events = [[NSMutableArray<TestKeyEvent*> alloc] init];
FlutterKeyEvent* event;
__block BOOL last_handled = TRUE;
id keyEventCallback = ^(BOOL handled) {
last_handled = handled;
@@ -1056,13 +1072,20 @@ TEST(FlutterEmbedderKeyResponderUnittests, SynchronizeCapsLockStateOnCapsLock) {
}];
// In: CapsLock down
// Out:
// Out: (empty)
last_handled = FALSE;
[responder handleEvent:keyEvent(NSEventTypeFlagsChanged, 0x100, @"", @"", FALSE, kKeyCodeCapsLock)
callback:keyEventCallback];
EXPECT_EQ([events count], 0u);
EXPECT_EQ([events count], 1u);
EXPECT_EQ(last_handled, TRUE);
event = [events lastObject].data;
EXPECT_EQ(event->physical, 0ull);
EXPECT_EQ(event->logical, 0ull);
EXPECT_FALSE([[events lastObject] hasCallback]);
EXPECT_EQ(last_handled, TRUE);
[events removeAllObjects];
}
// Press the CapsLock key when CapsLock state is desynchronized

View File

@@ -693,6 +693,14 @@ typedef enum {
/// released.
/// * All events throughout a key press sequence shall have the same `physical`
/// and `logical`. Having different `character`s is allowed.
///
/// A `FlutterKeyEvent` with `physical` 0 and `logical` 0 is an empty event.
/// This is the only case either `physical` or `logical` can be 0. An empty
/// event must be sent if a key message should be converted to no
/// `FlutterKeyEvent`s, for example, when a key down message is received for a
/// key that has already been pressed according to the record. This is to ensure
/// some `FlutterKeyEvent` arrives at the framework before raw key message.
/// See https://github.com/flutter/flutter/issues/87230.
typedef struct {
/// The size of this struct. Must be sizeof(FlutterKeyEvent).
size_t struct_size;
@@ -706,11 +714,17 @@ typedef struct {
///
/// For the full definition and list of pre-defined physical keys, see
/// `PhysicalKeyboardKey` from the framework.
///
/// The only case that `physical` might be 0 is when this is an empty event.
/// See `FlutterKeyEvent` for introduction.
uint64_t physical;
/// The key ID for the logical key of this event.
///
/// For the full definition and a list of pre-defined logical keys, see
/// `LogicalKeyboardKey` from the framework.
///
/// The only case that `logical` might be 0 is when this is an empty event.
/// See `FlutterKeyEvent` for introduction.
uint64_t logical;
/// Null-terminated character input from the event. Can be null. Ignored for
/// up events.

View File

@@ -14,6 +14,16 @@
constexpr uint64_t kMicrosecondsPerMillisecond = 1000;
static const FlutterKeyEvent empty_event{
.struct_size = sizeof(FlutterKeyEvent),
.timestamp = 0,
.type = kFlutterKeyEventTypeDown,
.physical = 0,
.logical = 0,
.character = nullptr,
.synthesized = false,
};
// Look up a hash table that maps a uint64_t to a uint64_t.
//
// Returns 0 if not found.
@@ -712,6 +722,7 @@ static void fl_key_embedder_responder_handle_event(
// pressed one, usually indicating multiple keyboards are pressing keys
// with the same physical key, or the up event was lost during a loss of
// focus. The down event is ignored.
fl_engine_send_key_event(self->engine, &empty_event, nullptr, nullptr);
callback(true, user_data);
return;
} else {
@@ -724,6 +735,7 @@ static void fl_key_embedder_responder_handle_event(
// The physical key has been released before. It might indicate a missed
// event due to loss of focus, or multiple keyboards pressed keys with the
// same physical key. Ignore the up event.
fl_engine_send_key_event(self->engine, &empty_event, nullptr, nullptr);
callback(true, user_data);
return;
} else {

View File

@@ -878,14 +878,23 @@ TEST(FlKeyEmbedderResponderTest, IgnoreDuplicateDownEvent) {
// Press KeyA again (with different logical key, which is not necessari but
// for coverage).
g_expected_handled = true; // The ignored event is always handled.
g_expected_handled = true; // The empty event is always handled.
fl_key_responder_handle_event(
responder,
fl_key_event_new_by_mock(102, kPress, GDK_KEY_q, kKeyCodeKeyA, 0,
kIsNotModifier),
verify_response_handled, &user_data);
EXPECT_EQ(g_call_records->len, 0u);
EXPECT_EQ(g_call_records->len, 1u);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 0));
EXPECT_EQ(record->event->physical, 0ull);
EXPECT_EQ(record->event->logical, 0ull);
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, false);
EXPECT_EQ(record->callback, nullptr);
g_ptr_array_clear(g_call_records);
// Release KeyA
fl_key_responder_handle_event(
@@ -905,6 +914,8 @@ TEST(FlKeyEmbedderResponderTest, IgnoreDuplicateDownEvent) {
}
TEST(FlKeyEmbedderResponderTest, IgnoreAbruptUpEvent) {
FlKeyEmbedderCallRecord* record;
EXPECT_EQ(g_call_records, nullptr);
g_call_records = g_ptr_array_new_with_free_func(g_object_unref);
FlEngine* engine = make_mock_engine_with_records();
@@ -913,14 +924,21 @@ TEST(FlKeyEmbedderResponderTest, IgnoreAbruptUpEvent) {
int user_data = 123; // Arbitrary user data
// Release KeyA before it was even pressed.
g_expected_handled = true; // The ignored event is always handled.
g_expected_handled = true; // The empty event is always handled.
fl_key_responder_handle_event(
responder,
fl_key_event_new_by_mock(103, kRelease, GDK_KEY_q, kKeyCodeKeyA, 0,
kIsNotModifier),
verify_response_handled, &user_data);
EXPECT_EQ(g_call_records->len, 0u);
EXPECT_EQ(g_call_records->len, 1u);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 0));
EXPECT_EQ(record->event->physical, 0ull);
EXPECT_EQ(record->event->logical, 0ull);
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, false);
EXPECT_EQ(record->callback, nullptr);
clear_g_call_records();
g_object_unref(engine);
@@ -1039,11 +1057,11 @@ TEST(FlKeyEmbedderResponderTest, SynthesizeForDesyncPressingStateOnSelfEvents) {
kKeyCodeControlRight, state, kIsModifier),
verify_response_handled, &user_data);
// A ControlLeft down is synthesized, with no non-synthesized event.
// A ControlLeft down is synthesized, with an empty event.
// Reason: The ControlLeft down is synthesized to synchronize the state
// showing Control as pressed. The ControlRight event is ignored because
// the event is considered a duplicate up event.
EXPECT_EQ(g_call_records->len, 1u);
EXPECT_EQ(g_call_records->len, 2u);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 0));
EXPECT_EQ(record->event->timestamp, 105000);
EXPECT_EQ(record->event->type, kFlutterKeyEventTypeDown);
@@ -1052,6 +1070,13 @@ TEST(FlKeyEmbedderResponderTest, SynthesizeForDesyncPressingStateOnSelfEvents) {
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, true);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 1));
EXPECT_EQ(record->event->physical, 0ull);
EXPECT_EQ(record->event->logical, 0ull);
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, false);
EXPECT_EQ(record->callback, nullptr);
g_ptr_array_clear(g_call_records);
clear_g_call_records();
@@ -1352,7 +1377,7 @@ TEST(FlKeyEmbedderResponderTest, SynthesizeForDesyncLockModeOnNonSelfEvents) {
g_ptr_array_clear(g_call_records);
// Release NumLock. Since the previous event should have synthesized NumLock
// to be released, this should result in no events.
// to be released, this should result in only an empty event.
g_expected_handled = true;
fl_key_responder_handle_event(
responder,
@@ -1360,7 +1385,13 @@ TEST(FlKeyEmbedderResponderTest, SynthesizeForDesyncLockModeOnNonSelfEvents) {
state, kIsModifier),
verify_response_handled, &user_data);
EXPECT_EQ(g_call_records->len, 0u);
EXPECT_EQ(g_call_records->len, 1u);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 0));
EXPECT_EQ(record->event->physical, 0ull);
EXPECT_EQ(record->event->logical, 0ull);
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, false);
EXPECT_EQ(record->callback, nullptr);
clear_g_call_records();
g_object_unref(engine);
@@ -1491,7 +1522,7 @@ TEST(FlKeyEmbedderResponderTest, SynthesizationOccursOnIgnoredEvents) {
kIsNotModifier),
verify_response_handled, &user_data);
EXPECT_EQ(g_call_records->len, 2u);
EXPECT_EQ(g_call_records->len, 3u);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 0));
EXPECT_EQ(record->event->timestamp, 101000);
EXPECT_EQ(record->event->type, kFlutterKeyEventTypeDown);
@@ -1508,6 +1539,13 @@ TEST(FlKeyEmbedderResponderTest, SynthesizationOccursOnIgnoredEvents) {
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, true);
record = FL_KEY_EMBEDDER_CALL_RECORD(g_ptr_array_index(g_call_records, 2));
EXPECT_EQ(record->event->physical, 0ull);
EXPECT_EQ(record->event->logical, 0ull);
EXPECT_STREQ(record->event->character, nullptr);
EXPECT_EQ(record->event->synthesized, false);
EXPECT_EQ(record->callback, nullptr);
g_ptr_array_clear(g_call_records);
clear_g_call_records();

View File

@@ -26,6 +26,7 @@ constexpr size_t kCharacterCacheSize = 8;
constexpr SHORT kStateMaskToggled = 0x01;
constexpr SHORT kStateMaskPressed = 0x80;
const char* empty_character = "";
} // namespace
KeyboardKeyEmbedderHandler::KeyboardKeyEmbedderHandler(
@@ -155,6 +156,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHook(
// as a currently pressed one, usually indicating multiple keyboards are
// pressing keys with the same physical key, or the up event was lost
// during a loss of focus. The down event is ignored.
sendEvent_(CreateEmptyEvent(), nullptr, nullptr);
callback(true);
return;
}
@@ -171,6 +173,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHook(
// The physical key has been released before. It might indicate a missed
// event due to loss of focus, or multiple keyboards pressed keys with the
// same physical key. Ignore the up event.
sendEvent_(CreateEmptyEvent(), nullptr, nullptr);
callback(true);
return;
} else {
@@ -200,6 +203,7 @@ void KeyboardKeyEmbedderHandler::KeyboardHook(
// presses are considered handled and not sent to Flutter. These events must
// be filtered by result_logical_key because the key up event of such
// presses uses the "original" logical key.
sendEvent_(CreateEmptyEvent(), nullptr, nullptr);
callback(true);
return;
}
@@ -272,7 +276,6 @@ void KeyboardKeyEmbedderHandler::SynchronizeCritialToggledStates(
key_info.toggled_on = !key_info.toggled_on;
}
if (key_info.toggled_on != should_toggled) {
const char* empty_character = "";
// If the key is pressed, release it first.
if (pressingRecords_.find(key_info.physical_key) !=
pressingRecords_.end()) {
@@ -390,6 +393,21 @@ void KeyboardKeyEmbedderHandler::ConvertUtf32ToUtf8_(char* out, char32_t ch) {
strcpy_s(out, kCharacterCacheSize, Utf8FromUtf16(text).c_str());
}
FlutterKeyEvent KeyboardKeyEmbedderHandler::CreateEmptyEvent() {
return FlutterKeyEvent{
.struct_size = sizeof(FlutterKeyEvent),
.timestamp = static_cast<double>(
std::chrono::duration_cast<std::chrono::microseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch())
.count()),
.type = kFlutterKeyEventTypeDown,
.physical = 0,
.logical = 0,
.character = empty_character,
.synthesized = false,
};
}
FlutterKeyEvent KeyboardKeyEmbedderHandler::SynthesizeSimpleEvent(
FlutterKeyEventType type,
uint64_t physical,

View File

@@ -21,6 +21,12 @@ namespace {} // namespace
// converted |FlutterKeyEvent|s through the embedder API.
//
// This class communicates with the HardwareKeyboard API in the framework.
//
// Every key event must result in at least one FlutterKeyEvent, even an empty
// one (both logical and physical IDs are 0). This ensures that raw key
// messages are always preceded by key data so that the transit mode is
// correctly inferred. (Technically only the first key event needs so, but for
// simplicity.)
class KeyboardKeyEmbedderHandler
: public KeyboardKeyHandler::KeyboardKeyHandlerDelegate {
public:
@@ -117,6 +123,11 @@ class KeyboardKeyEmbedderHandler
static uint64_t GetLogicalKey(int key, bool extended, int scancode);
static void HandleResponse(bool handled, void* user_data);
static void ConvertUtf32ToUtf8_(char* out, char32_t ch);
// Create an empty event.
//
// This is used when no key data needs to be sent. For the reason, see the
// |KeyboardKeyEmbedderHandler| class.
static FlutterKeyEvent CreateEmptyEvent();
static FlutterKeyEvent SynthesizeSimpleEvent(FlutterKeyEventType type,
uint64_t physical,
uint64_t logical,

View File

@@ -247,6 +247,15 @@ TEST(KeyboardKeyEmbedderHandlerTest, ImeEventsAreIgnored) {
[&last_handled](bool handled) { last_handled = handled; });
EXPECT_EQ(last_handled, true);
// The A key down should yield an empty event.
EXPECT_EQ(results.size(), 1);
event = &results[0];
EXPECT_EQ(event->physical, 0);
EXPECT_EQ(event->logical, 0);
EXPECT_EQ(event->callback, nullptr);
results.clear();
// Release A in an IME
last_handled = false;
handler->KeyboardHook(
// The up event for an IME press has a normal virtual key.
@@ -254,8 +263,13 @@ TEST(KeyboardKeyEmbedderHandlerTest, ImeEventsAreIgnored) {
[&last_handled](bool handled) { last_handled = handled; });
EXPECT_EQ(last_handled, true);
// The entire A press does not yield events.
EXPECT_EQ(results.size(), 0);
// The A key up should yield an empty event.
EXPECT_EQ(results.size(), 1);
event = &results[0];
EXPECT_EQ(event->physical, 0);
EXPECT_EQ(event->logical, 0);
EXPECT_EQ(event->callback, nullptr);
results.clear();
// Press A out of an IME
key_state.Set(kVirtualKeyA, true);
@@ -469,6 +483,54 @@ TEST(KeyboardKeyEmbedderHandlerTest, ModifierKeysByVirtualKey) {
results.clear();
}
TEST(KeyboardKeyEmbedderHandlerTest, RepeatedDownIsIgnored) {
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());
last_handled = false;
// Press A (should yield a normal event)
handler->KeyboardHook(
kVirtualKeyA, kScanCodeKeyA, WM_KEYDOWN, 'a', false, false,
[&last_handled](bool handled) { last_handled = handled; });
EXPECT_EQ(last_handled, false);
EXPECT_EQ(results.size(), 1);
event = &results[0];
EXPECT_EQ(event->type, kFlutterKeyEventTypeDown);
EXPECT_EQ(event->physical, kPhysicalKeyA);
EXPECT_EQ(event->logical, kLogicalKeyA);
EXPECT_STREQ(event->character, "a");
EXPECT_EQ(event->synthesized, false);
event->callback(true, event->user_data);
EXPECT_EQ(last_handled, true);
results.clear();
// KeyA's key up is missed.
// Press A again (should yield an empty event)
last_handled = false;
handler->KeyboardHook(
kVirtualKeyA, kScanCodeKeyA, WM_KEYDOWN, 'a', false, false,
[&last_handled](bool handled) { last_handled = handled; });
EXPECT_EQ(last_handled, true);
EXPECT_EQ(results.size(), 1);
event = &results[0];
EXPECT_EQ(event->physical, 0);
EXPECT_EQ(event->logical, 0);
EXPECT_EQ(event->callback, nullptr);
results.clear();
}
TEST(KeyboardKeyEmbedderHandlerTest, AbruptRepeatIsConvertedToDown) {
TestKeystate key_state;
std::vector<TestFlutterKeyEvent> results;
@@ -523,6 +585,39 @@ TEST(KeyboardKeyEmbedderHandlerTest, AbruptRepeatIsConvertedToDown) {
results.clear();
}
TEST(KeyboardKeyEmbedderHandlerTest, AbruptUpIsIgnored) {
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());
last_handled = false;
// KeyA's key down is missed.
key_state.Set(kVirtualKeyA, true);
// Press A again (should yield an empty event)
last_handled = false;
handler->KeyboardHook(
kVirtualKeyA, kScanCodeKeyA, WM_KEYUP, 'a', false, true,
[&last_handled](bool handled) { last_handled = handled; });
EXPECT_EQ(last_handled, true);
EXPECT_EQ(results.size(), 1);
event = &results[0];
EXPECT_EQ(event->physical, 0);
EXPECT_EQ(event->logical, 0);
EXPECT_EQ(event->callback, nullptr);
results.clear();
}
TEST(KeyboardKeyEmbedderHandlerTest, SynthesizeForDesyncPressingState) {
TestKeystate key_state;
std::vector<TestFlutterKeyEvent> results;