gpt4 book ai didi

macos - 将虚拟按键代码转换为 unicode 字符串

转载 作者:行者123 更新时间:2023-12-03 16:05:12 25 4
gpt4 key购买 nike

我有一些代码一直用来获取当前的键盘布局并将虚拟键代码转换为字符串。这在大多数情况下都很有效,但我在某些特定情况下遇到了麻烦。揭示这一点的是德国 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];
}

最佳答案

该键一个死键,如果您自己尝试一下或查看处于事件状态的德语布局的键盘查看器,您会发现这一点。

在 Mac 上,输入死键的实际字符而不与其他字符组合的方法是在其后按空格。所以尝试一下:关闭 kUCKeyTranslateNoDeadKeysBit ,如果 UCKeyTranslate 设置了死键状态,则在其后面平移一个空格。

编辑(由提问者添加)

对于 future 的人们,这里是具有正确解决方案的固定代码。

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];
}

关于macos - 将虚拟按键代码转换为 unicode 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8263618/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com