我有一些代码,用于获取当前键盘布局并将虚拟键码转换为字符串。在大多数情况下这很有效,但我在某些特定情况下遇到了麻烦。让我注意到这个问题的是德语QWERTZ键盘上退格键旁边的重音键。 http://en.wikipedia.org/wiki/File:KB_Germany.svg
该键生成我期望的VK代码
我的当前代码如下。
kVK_ANSI_Equal
,但在使用QWERTZ键盘布局时,我没有得到任何描述。它最终成为一个死键,因为它应该与另一个键组合。有没有办法捕获这些情况并进行适当的转换?我的当前代码如下。
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];
}