我有一些代碼,我一直在使用獲取當前的鍵盤佈局,並將虛擬鍵碼轉換爲字符串。這在大多數情況下效果很好,但我在某些特定情況下遇到問題。讓這個燈亮的是德國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