Skip to content

Commit

Permalink
fix: modifier change event in older macOS
Browse files Browse the repository at this point in the history
event.keyCode is available since macOS 0.15.

fixes #715
  • Loading branch information
lotem committed Feb 5, 2023
1 parent 3bc6c2c commit 5c2b7e6
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions SquirrelInputController.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,32 +78,53 @@ - (BOOL)handleEvent:(NSEvent*)event client:(id)sender
handled = YES;
break;
}
int keyCode = event.keyCode;
//NSLog(@"FLAGSCHANGED client: %@, modifiers: 0x%lx, keyCode: %d", sender, modifiers, keyCode);
int rime_keycode = osx_keycode_to_rime_keycode(keyCode, 0, 0, 0);
//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;
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;
[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;
[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;
[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;
[self processKey:rime_keycode modifiers:(rime_modifiers | release_mask)];
// do not update UI when using Command key
Expand Down

0 comments on commit 5c2b7e6

Please sign in to comment.