Use UCKeyTranslate if the 'uchr' resource exists. If not, fall back on the 'KCHR' resource.

This commit is contained in:
Steve Checkoway
2006-09-14 09:13:46 +00:00
parent ad809ec5fa
commit 5083893317
@@ -374,6 +374,49 @@ wchar_t InputHandler_Carbon::DeviceButtonToChar( DeviceButton button, bool bUseC
if( bUseCurrentKeyModifiers ) if( bUseCurrentKeyModifiers )
modifiers = GetCurrentKeyModifiers(); modifiers = GetCurrentKeyModifiers();
SInt16 iCurrentKeyScript = GetScriptManagerVariable( smKeyScript );
SInt16 iCurrentKeyLayoutID = GetScriptVariable( iCurrentKeyScript, smScriptKeys );
static SInt16 iLastKeyLayoutID = !iCurrentKeyLayoutID; // Just be different.
static UInt32 iDeadKeyState;
static UCKeyboardLayout **KeyLayout;
if( iCurrentKeyLayoutID != iLastKeyLayoutID )
{
iDeadKeyState = 0;
KeyLayout = (UCKeyboardLayout **)GetResource( 'uchr', iCurrentKeyLayoutID );
iLastKeyLayoutID = iCurrentKeyLayoutID;
}
if( KeyLayout )
{
UInt32 keyboardType = LMGetKbdType();
UInt32 modifiers = bUseCurrentKeyModifiers ? GetCurrentKeyModifiers() : 0;
UniChar unicodeInputString[4];
UniCharCount length;
OSStatus status = UCKeyTranslate( *KeyLayout, iMacVirtualKey, kUCKeyActionDown, modifiers,
keyboardType, 0, &iDeadKeyState, ARRAYLEN(unicodeInputString),
&length, unicodeInputString );
if( status )
return L'\0';
CFStringRef inputString = CFStringCreateWithCharacters( NULL, unicodeInputString, length );
char utf8InputString[7]; // Max size is 6 (although really only 4 are used) + null.
if( !CFStringGetCString(inputString, utf8InputString, 7, kCFStringEncodingUTF8) )
{
CFRelease( inputString );
return L'\0';
}
wchar_t ch = utf8_get_char( utf8InputString );
CFRelease( inputString );
return ch == INVALID_CHAR ? L'\0' : ch;
}
else
{
// Fall back on the 'KCHR' resource.
static unsigned long state = 0; static unsigned long state = 0;
static Ptr keymap = NULL; static Ptr keymap = NULL;
Ptr new_keymap; Ptr new_keymap;
@@ -384,9 +427,9 @@ wchar_t InputHandler_Carbon::DeviceButtonToChar( DeviceButton button, bool bUseC
keymap = new_keymap; keymap = new_keymap;
state = 0; state = 0;
} }
// TODO: Use UCKeyTranslate // XXX: Only returns ascii. Ž will be returned as e.
wchar_t ch = KeyTranslate( keymap, ((int)iMacVirtualKey)|modifiers, &state ) & 0xFFFF; return KeyTranslate( keymap, UInt16(iMacVirtualKey)|modifiers, &state ) & 0xFF;
return ch; }
} }
return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers ); return InputHandler::DeviceButtonToChar( button, bUseCurrentKeyModifiers );