Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 9f175fd

Browse files
authored
Do not generate keydown event for empty modifier flags (#27817)
* Do not generate keydown event for empty modifier flags * Mask 0x100 bit instead of comparison * Comment * Update comment * comment * Add comment
1 parent 37e8b8b commit 9f175fd

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponder.mm

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ - (nonnull instancetype)initWithChannel:(nonnull FlutterBasicMessageChannel*)cha
3939
}
4040

4141
- (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
42+
// Remove the modifier bits that Flutter is not interested in.
43+
NSEventModifierFlags modifierFlags = event.modifierFlags & ~0x100;
4244
NSString* type;
4345
switch (event.type) {
4446
case NSEventTypeKeyDown:
@@ -48,9 +50,9 @@ - (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
4850
type = @"keyup";
4951
break;
5052
case NSEventTypeFlagsChanged:
51-
if (event.modifierFlags < _previouslyPressedFlags) {
53+
if (modifierFlags < _previouslyPressedFlags) {
5254
type = @"keyup";
53-
} else if (event.modifierFlags > _previouslyPressedFlags) {
55+
} else if (modifierFlags > _previouslyPressedFlags) {
5456
type = @"keydown";
5557
} else {
5658
// ignore duplicate modifiers; This can happen in situations like switching
@@ -61,7 +63,7 @@ - (void)handleEvent:(NSEvent*)event callback:(FlutterAsyncKeyCallback)callback {
6163
default:
6264
NSAssert(false, @"Unexpected key event type (got %lu).", event.type);
6365
}
64-
_previouslyPressedFlags = event.modifierFlags;
66+
_previouslyPressedFlags = modifierFlags;
6567
NSMutableDictionary* keyMessage = [@{
6668
@"keymap" : @"macos",
6769
@"type" : type,

shell/platform/darwin/macos/framework/Source/FlutterChannelKeyResponderUnittests.mm

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,20 @@
5252
callback(keyMessage);
5353
}));
5454

55-
// Key down
5655
FlutterChannelKeyResponder* responder =
5756
[[FlutterChannelKeyResponder alloc] initWithChannel:mockKeyEventChannel];
57+
58+
// Initial empty modifiers. This can happen when user opens window while modifier key is pressed
59+
// and then releases the modifier. Shouldn't result in an event being sent.
60+
// Regression test for https://github.com/flutter/flutter/issues/87339.
61+
[responder handleEvent:keyEvent(NSEventTypeFlagsChanged, 0x100, @"", @"", FALSE, 60)
62+
callback:^(BOOL handled) {
63+
[responses addObject:@(handled)];
64+
}];
65+
66+
EXPECT_EQ([messages count], 0u);
67+
68+
// Key down
5869
[responder handleEvent:keyEvent(NSEventTypeKeyDown, 0x100, @"a", @"a", FALSE, 0)
5970
callback:^(BOOL handled) {
6071
[responses addObject:@(handled)];

0 commit comments

Comments
 (0)