diff --git a/SquirrelInputController.m b/SquirrelInputController.m index 02f0ef8a4..6746a24df 100644 --- a/SquirrelInputController.m +++ b/SquirrelInputController.m @@ -24,7 +24,7 @@ @implementation SquirrelInputController { NSUInteger _caretPos; NSArray *_candidates; NSUInteger _lastModifier; - NSEventType _lastEventType; + int _lastEventCount; RimeSessionId _session; NSString *_schemaId; BOOL _inlinePreedit; @@ -51,9 +51,12 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender // Returning NO means the original key down will be passed on to the client. _currentClient = sender; - - NSUInteger modifiers = event.modifierFlags; - + NSEventModifierFlags modifiers = event.modifierFlags & NSEventModifierFlagDeviceIndependentFlagsMask; + int eventCount = CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventFlagsChanged) + + CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventKeyDown) + + CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventLeftMouseDown) + + CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventRightMouseDown) + + CGEventSourceCounterForEventType(kCGEventSourceStateCombinedSessionState, kCGEventOtherMouseDown); BOOL handled = NO; @autoreleasepool { @@ -78,56 +81,33 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender break; } //NSLog(@"FLAGSCHANGED client: %@, modifiers: 0x%lx", sender, modifiers); - int rime_modifiers = osx_modifiers_to_rime_modifiers(modifiers); - int rime_keycode = 0; - // For flags-changed event, keyCode is available since macOS 10.15 (#715) - Bool keyCodeAvailable = NO; - if (@available(macOS 10.15, *)) { - keyCodeAvailable = YES; - rime_keycode = osx_keycode_to_rime_keycode(event.keyCode, 0, 0, 0); - //NSLog(@"keyCode: %d", event.keyCode); - } int release_mask = 0; NSUInteger changes = _lastModifier ^ modifiers; + int rime_modifiers = osx_modifiers_to_rime_modifiers(modifiers); + CGKeyCode keyCode = CGEventGetIntegerValueField(event.CGEvent, kCGKeyboardEventKeycode); + int rime_keycode = osx_keycode_to_rime_keycode(keyCode, 0, 0, 0); + if (changes & OSX_CAPITAL_MASK) { - if (!keyCodeAvailable) { - rime_keycode = XK_Caps_Lock; - } - // NOTE: rime assumes XK_Caps_Lock to be sent before modifier changes, - // while NSFlagsChanged event has the flag changed already. - // so it is necessary to revert kLockMask. rime_modifiers ^= kLockMask; [self processKey:rime_keycode modifiers:rime_modifiers]; } if (changes & OSX_SHIFT_MASK) { - if (!keyCodeAvailable) { - rime_keycode = XK_Shift_L; - } - release_mask = modifiers & OSX_SHIFT_MASK ? 0 : kReleaseMask; + release_mask = modifiers & OSX_SHIFT_MASK ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask); [self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)]; } if (changes & OSX_CTRL_MASK) { - if (!keyCodeAvailable) { - rime_keycode = XK_Control_L; - } - release_mask = modifiers & OSX_CTRL_MASK ? 0 : kReleaseMask; + release_mask = modifiers & OSX_CTRL_MASK ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask); [self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)]; } if (changes & OSX_ALT_MASK) { - if (!keyCodeAvailable) { - rime_keycode = XK_Alt_L; - } - release_mask = modifiers & OSX_ALT_MASK ? 0 : kReleaseMask; + release_mask = modifiers & OSX_ALT_MASK ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask); [self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)]; } if (changes & OSX_COMMAND_MASK) { - if (!keyCodeAvailable) { - rime_keycode = XK_Super_L; - } - release_mask = modifiers & OSX_COMMAND_MASK ? 0 : kReleaseMask; + release_mask = modifiers & OSX_COMMAND_MASK ? 0 : kReleaseMask | (eventCount - _lastEventCount == 1 ? 0 : kIgnoredMask); [self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)]; // do not update UI when using Command key - break; + goto saveStatus; } [self rimeUpdate]; } break; @@ -158,10 +138,10 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender break; } } - - _lastModifier = modifiers; - _lastEventType = event.type; - + saveStatus: if (event.type == NSEventTypeFlagsChanged) { + _lastModifier = modifiers; + _lastEventCount = eventCount; + } return handled; } diff --git a/librime b/librime index 8911db66b..b87eb81ce 160000 --- a/librime +++ b/librime @@ -1 +1 @@ -Subproject commit 8911db66b0e5d43c2c3e47ffec06bfd10201daae +Subproject commit b87eb81cec851094e7f16a73ca9c453f3664d6e2