Skip to content

Commit

Permalink
Do not generate keydown event for empty modifier flags (flutter#27817)
Browse files Browse the repository at this point in the history
* Do not generate keydown event for empty modifier flags

* Mask 0x100 bit instead of comparison

* Comment

* Update comment

* comment

* Add comment
  • Loading branch information
knopp authored and filmil committed Apr 21, 2022
1 parent 6a75f9f commit ab4d9a9
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ - (nonnull instancetype)initWithChannel:(nonnull FlutterBasicMessageChannel*)cha
}

- (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:
Expand All @@ -48,9 +50,9 @@ - (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
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
Expand All @@ -61,7 +63,7 @@ - (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
default:
NSAssert(false, @"Unexpected key event type (got %lu).", event.type);
}
_previouslyPressedFlags = event.modifierFlags;
_previouslyPressedFlags = modifierFlags;
NSMutableDictionary* keyMessage = [@{
@"keymap" : @"macos",
@"type" : type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,20 @@
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)];
Expand Down

0 comments on commit ab4d9a9

Please sign in to comment.