Convert virtual Key Code to unicode string

I have a code that I used to get the current keyboard layout and convert the virtual key code to a string. In most cases, this works fine, but I am having problems with some specific cases. The one that revealed this is the accent key next to the backspace key on German QWERTZ keyboards. http://en.wikipedia.org/wiki/File:KB_Germany.svg

This key generates the VK code, which I would expect kVK_ANSI_Equal , but when using the QWERTZ keyboard layout, I do not get a description. Its end is like a dead key, because it must be composed with another key. Is there a way to catch these cases and make the right conversion?

My current code is below.

 TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr); if(keyboardLayout) { UInt32 deadKeyState = 0; UniCharCount maxStringLength = 255; UniCharCount actualStringLength = 0; UniChar unicodeString[maxStringLength]; OSStatus status = UCKeyTranslate(keyboardLayout, keyCode, kUCKeyActionDown, 0, LMGetKbdType(), kUCKeyTranslateNoDeadKeysBit, &deadKeyState, maxStringLength, &actualStringLength, unicodeString); if(actualStringLength > 0 && status == noErr) return [[NSString stringWithCharacters:unicodeString length:(NSInteger)actualStringLength] uppercaseString]; } 
+7
cocoa localization carbon keyboard macos
Nov 24 2018-11-11T00:
source share
1 answer

This key is a dead key, as you can see if you try it yourself or look at a keyboard viewer with an active German layout.

On a Mac, the way to enter the actual dead key character without composing it with another character is to press the space bar after it. So try: Disable kUCKeyTranslateNoDeadKeysBit , and if UCKeyTranslate sets the dead key state, translate the space after it.

EDIT (added by user)

Just for future people, here is a fixed code with the right solution.

 TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource(); CFDataRef uchr = (CFDataRef)TISGetInputSourceProperty(currentKeyboard, kTISPropertyUnicodeKeyLayoutData); const UCKeyboardLayout *keyboardLayout = (const UCKeyboardLayout*)CFDataGetBytePtr(uchr); if(keyboardLayout) { UInt32 deadKeyState = 0; UniCharCount maxStringLength = 255; UniCharCount actualStringLength = 0; UniChar unicodeString[maxStringLength]; OSStatus status = UCKeyTranslate(keyboardLayout, keyCode, kUCKeyActionDown, 0, LMGetKbdType(), 0, &deadKeyState, maxStringLength, &actualStringLength, unicodeString); if (actualStringLength == 0 && deadKeyState) { status = UCKeyTranslate(keyboardLayout, kVK_Space, kUCKeyActionDown, 0, LMGetKbdType(), 0, &deadKeyState, maxStringLength, &actualStringLength, unicodeString); } if(actualStringLength > 0 && status == noErr) return [[NSString stringWithCharacters:unicodeString length:(NSUInteger)actualStringLength] uppercaseString]; } 
+13
Nov 25 2018-11-11T00:
source share
— -



All Articles