Do not generate keydown event for empty modifier flags (flutter/engine#27817)

* Do not generate keydown event for empty modifier flags

* Mask 0x100 bit instead of comparison

* Comment

* Update comment

* comment

* Add comment
This commit is contained in:
Matej Knopp
2021-07-31 03:00:54 +02:00
committed by GitHub
parent 1fce953e6d
commit 7cf7937a38
2 changed files with 17 additions and 4 deletions

View File

@@ -39,6 +39,8 @@
}
- (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
// Remove the modifier bits that Flutter is not interested in.
NSEventModifierFlags modifierFlags = event.modifierFlags & ~0x100;
NSString* type;
switch (event.type) {
case NSEventTypeKeyDown:
@@ -48,9 +50,9 @@
type = @"keyup";
break;
case NSEventTypeFlagsChanged:
if (event.modifierFlags < _previouslyPressedFlags) {
if (modifierFlags < _previouslyPressedFlags) {
type = @"keyup";
} else if (event.modifierFlags > _previouslyPressedFlags) {
} else if (modifierFlags > _previouslyPressedFlags) {
type = @"keydown";
} else {
// ignore duplicate modifiers; This can happen in situations like switching
@@ -61,7 +63,7 @@
default:
NSAssert(false, @"Unexpected key event type (got %lu).", event.type);
}
_previouslyPressedFlags = event.modifierFlags;
_previouslyPressedFlags = modifierFlags;
NSMutableDictionary* keyMessage = [@{
@"keymap" : @"macos",
@"type" : type,

View File

@@ -52,9 +52,20 @@ TEST(FlutterChannelKeyResponderUnittests, BasicKeyEvent) {
callback(keyMessage);
}));
// Key down
FlutterChannelKeyResponder* responder =
[[FlutterChannelKeyResponder alloc] initWithChannel:mockKeyEventChannel];
// Initial empty modifiers. This can happen when user opens window while modifier key is pressed
// and then releases the modifier. Shouldn't result in an event being sent.
// Regression test for https://github.com/flutter/flutter/issues/87339.
[responder handleEvent:keyEvent(NSEventTypeFlagsChanged, 0x100, @"", @"", FALSE, 60)
callback:^(BOOL handled) {
[responses addObject:@(handled)];
}];
EXPECT_EQ([messages count], 0u);
// Key down
[responder handleEvent:keyEvent(NSEventTypeKeyDown, 0x100, @"a", @"a", FALSE, 0)
callback:^(BOOL handled) {
[responses addObject:@(handled)];