Jos*_*erg 8 keyboard macos cocoa localization macos-carbon
我有一些代码,我一直用来获取当前的键盘布局,并将虚拟键码转换为字符串.这在大多数情况下效果很好,但我在某些特定情况下遇到了麻烦.引起这种情况的是德国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)
Pet*_*sey 14
该键是一个死键,您可以看到,如果您自己尝试或查看具有德语布局活动的键盘查看器.
在Mac上,输入死键的实际角色而不用另一个角色组成的方法是按下后面的空格.因此,请尝试:关闭kUCKeyTranslateNoDeadKeysBit,如果UCKeyTranslate设置死键状态,请在其后转换空格.
编辑(由提问者添加)
仅为未来的人们,这里是具有正确解决方案的固定代码.
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];
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4086 次 |
| 最近记录: |