cocoadisplay: Fix handling of shift modifier

Closes #967
Fixes #959
This commit is contained in:
Donny Lawrence 2020-06-30 17:36:01 -05:00 committed by rdb
parent 2cb5b95d08
commit 5d6ff0ecc8
1 changed files with 16 additions and 9 deletions

View File

@ -1720,20 +1720,19 @@ handle_key_event(NSEvent *event) {
return;
}
TISInputSourceRef input_source = TISCopyCurrentKeyboardLayoutInputSource();
CFDataRef layout_data = (CFDataRef)TISGetInputSourceProperty(input_source, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);
if ([event type] == NSKeyDown) {
// Translate it to a unicode character for keystrokes. I would use
// interpretKeyEvents and insertText, but that doesn't handle dead keys.
TISInputSourceRef input_source = TISCopyCurrentKeyboardLayoutInputSource();
CFDataRef layout_data = (CFDataRef)TISGetInputSourceProperty(input_source, kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *layout = (const UCKeyboardLayout *)CFDataGetBytePtr(layout_data);
UInt32 modifier_state = (modifierFlags >> 16) & 0xFF;
UniChar ustr[8];
UniCharCount length;
UCKeyTranslate(layout, [event keyCode], kUCKeyActionDown, modifier_state,
LMGetKbdType(), 0, &_dead_key_state, sizeof(ustr), &length, ustr);
CFRelease(input_source);
for (int i = 0; i < length; ++i) {
UniChar c = ustr[i];
@ -1749,17 +1748,25 @@ handle_key_event(NSEvent *event) {
}
}
NSString *str = [event charactersIgnoringModifiers];
if (str == nil || [str length] == 0) {
// [NSEvent charactersIgnoringModifiers] doesn't ignore the shift key, so we
// need to do what that method is doing manually.
_dead_key_state = 0;
UniChar c;
UniCharCount length;
UCKeyTranslate(layout, [event keyCode], kUCKeyActionDisplay,
0, LMGetKbdType(), kUCKeyTranslateNoDeadKeysMask, &_dead_key_state,
sizeof(c), &length, &c);
CFRelease(input_source);
if (length != 1) {
return;
}
nassertv_always([str length] == 1);
unichar c = [str characterAtIndex: 0];
ButtonHandle button = map_key(c);
if (button == ButtonHandle::none()) {
// That done, continue trying to find out the button handle.
NSString *str = [[NSString alloc] initWithCharacters:&c length:length];
if ([str canBeConvertedToEncoding: NSASCIIStringEncoding]) {
// Nhm, ascii character perhaps?
str = [str lowercaseString];