Skip to content

Commit 04731c7

Browse files
committed
macos: try harder to avoid keyboard stuck state
1 parent dc60ca2 commit 04731c7

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

ui/drivers/ui_cocoa.m

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@
4141
#include <ApplicationServices/ApplicationServices.h>
4242
#endif
4343

44+
/* For NX_DEVICE*KEYMASK - the device-specific L/R modifier-key bits that
45+
* ride in NSEvent.modifierFlags alongside the coalesced high-order bits. */
46+
#include <IOKit/hidsystem/IOLLEvent.h>
47+
4448
#if defined(HAVE_COCOA_METAL)
4549
#include "../../gfx/common/metal_common.h"
4650
#endif
@@ -456,9 +460,26 @@ - (void)sendEvent:(NSEvent *)event {
456460
break;
457461
case NSEventTypeFlagsChanged:
458462
{
463+
/* Bits we treat as modifier-key transitions: the eight device-
464+
* specific L/R masks from IOKit plus CapsLock (which has no
465+
* device-specific bit). Everything else in modifierFlags is
466+
* metadata - notably kCGEventFlagMaskNonCoalesced (0x100) - and
467+
* must not be interpreted as a key press, or we end up
468+
* synthesising phantom events (a toggle of 0x100 with kc=0 used
469+
* to translate to MAC_NATIVE_TO_HID[0]==KEY_A, stuck forever). */
470+
static const NSUInteger mod_mask =
471+
NX_DEVICELCTLKEYMASK
472+
| NX_DEVICELSHIFTKEYMASK
473+
| NX_DEVICERSHIFTKEYMASK
474+
| NX_DEVICELCMDKEYMASK
475+
| NX_DEVICERCMDKEYMASK
476+
| NX_DEVICELALTKEYMASK
477+
| NX_DEVICERALTKEYMASK
478+
| NX_DEVICERCTLKEYMASK
479+
| NSEventModifierFlagCapsLock;
459480
static NSUInteger old_flags = 0;
460481
NSUInteger new_flags = [event modifierFlags];
461-
NSUInteger changed_flags = new_flags ^ old_flags;
482+
NSUInteger changed_flags = (new_flags ^ old_flags) & mod_mask;
462483
uint16_t keycode = [event keyCode];
463484
bool down = false;
464485

0 commit comments

Comments
 (0)