我有一些代码,我一直用来获取当前的键盘布局,并将虚拟键码转换为字符串.这在大多数情况下效果很好,但我在某些特定情况下遇到了麻烦.引起这种情况的是德国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];
}
Run Code Online (Sandbox Code Playgroud) 如何将密钥代码转换为kVK_ANSI_1我可以传递给的字符串setKeyEquivalent(所以kVK_ANSI_1,我会得到@"1")?为什么有两种方法来指定键呢?只有一个表示更有意义.