Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Coalesced modifier #763

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
60 changes: 20 additions & 40 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ @implementation SquirrelInputController {
NSUInteger _caretPos;
NSArray *_candidates;
NSUInteger _lastModifier;
NSEventType _lastEventType;
int _lastEventCount;
RimeSessionId _session;
NSString *_schemaId;
BOOL _inlinePreedit;
Expand All @@ -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 {
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion librime