我有一些代码,我一直在使用获取当前的键盘布局,并将虚拟键码转换为字符串。这在大多数情况下效果很好,但我在某些特定情况下遇到问题。让这个灯亮的是德国QWERTZ键盘上退格键旁边的重音键。 http://en.wikipedia.org/wiki/File:KB_Germany.svg转换虚拟键码到Unicode字符串
该键生成的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];
}
如果您没有设置kUCKeyTranslateNoDeadKeysMask代替kUCKeyTranslateNoDeadKeysBit,因为而前者与该位掩码启用实际上后者被定义为0? – rdb