From 75658109bebade083eb77b3a07718a3eb4d5d095 Mon Sep 17 00:00:00 2001 From: Germano Cavalcante Date: Wed, 12 Jul 2023 20:06:43 +0200 Subject: [PATCH] Fix #109630: Grave and Single Quote keys not being detected on MacOS The `UCKeyTranslate` function was being used wrong. The `deadKeyState` param should use `kUCKeyTranslateNoDeadKeysMask` instead of `kUCKeyTranslateNoDeadKeysBit` (optionally could also use `(1 << kUCKeyTranslateNoDeadKeysBit`)). This commit also dispenses with accessing the keyAction, as this is not crucial for determining the key. Comments have also been added to better describe the code. Pull Request: https://projects.blender.org/blender/blender/pulls/109987 --- intern/ghost/intern/GHOST_SystemCocoa.mm | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/intern/ghost/intern/GHOST_SystemCocoa.mm b/intern/ghost/intern/GHOST_SystemCocoa.mm index 1aaff5f2730..499c286d4be 100644 --- a/intern/ghost/intern/GHOST_SystemCocoa.mm +++ b/intern/ghost/intern/GHOST_SystemCocoa.mm @@ -77,7 +77,7 @@ static GHOST_TButton convertButton(int button) * \param recvChar: the character ignoring modifiers (except for shift) * \return Ghost key code */ -static GHOST_TKey convertKey(int rawCode, unichar recvChar, UInt16 keyAction) +static GHOST_TKey convertKey(int rawCode, unichar recvChar) { // printf("\nrecvchar %c 0x%x",recvChar,recvChar); switch (rawCode) { @@ -238,7 +238,10 @@ static GHOST_TKey convertKey(int rawCode, unichar recvChar, UInt16 keyAction) return GHOST_kKeyUpPage; case kVK_PageDown: return GHOST_kKeyDownPage; -#if 0 /* TODO: why are these commented? */ +#if 0 + /* These constants with "ANSI" in the name are labeled according to the key position on an + * ANSI-standard US keyboard. Therefore they may not match the physical key label on other + * keyboard layouts. */ case kVK_ANSI_Minus: return GHOST_kKeyMinus; case kVK_ANSI_Equal: return GHOST_kKeyEqual; case kVK_ANSI_Comma: return GHOST_kKeyComma; @@ -284,10 +287,10 @@ static GHOST_TKey convertKey(int rawCode, unichar recvChar, UInt16 keyAction) UCKeyTranslate((UCKeyboardLayout *)CFDataGetBytePtr(uchrHandle), rawCode, - keyAction, + kUCKeyActionDown, 0, LMGetKbdType(), - kUCKeyTranslateNoDeadKeysBit, + kUCKeyTranslateNoDeadKeysMask, &deadKeyState, 1, &actualStrLength, @@ -1830,18 +1833,13 @@ GHOST_TSuccess GHOST_SystemCocoa::handleKeyEvent(void *eventPtr) case NSEventTypeKeyDown: case NSEventTypeKeyUp: + /* Returns an empty string for dead keys. */ charsIgnoringModifiers = [event charactersIgnoringModifiers]; if ([charsIgnoringModifiers length] > 0) { - keyCode = convertKey([event keyCode], - [charsIgnoringModifiers characterAtIndex:0], - [event type] == NSEventTypeKeyDown ? kUCKeyActionDown : - kUCKeyActionUp); + keyCode = convertKey([event keyCode], [charsIgnoringModifiers characterAtIndex:0]); } else { - keyCode = convertKey([event keyCode], - 0, - [event type] == NSEventTypeKeyDown ? kUCKeyActionDown : - kUCKeyActionUp); + keyCode = convertKey([event keyCode], 0); } characters = [event characters];