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

标签 macos cocoa localization keyboard 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];
}

最佳答案

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

在 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/

相关文章:

objective-c - ReactiveCocoa 根据 NSTableView 选择状态启用 NSButton

swift - 如何检测 AppKit 应用程序是否已通过文档启动?

swift - 在类中创建自定义操作以在 Interface Builder 中使用

excel - Solver.xlam 丢失?

macos - 命令行工具 bash (git) 无法工作 - macOS Sierra 最终候选版本

swift - 按钮鼠标松开事件期间鼠标移动事件

ios - 如何解决本地化问题?

angularjs - angularjs过滤'date'和$ locale服务

数据库设计 : how to store translated numbers?

c++ - 如何在 Mac 上使用 c++2a 编译生成器和协程