gpt4 book ai didi

macos - 将按键代码转换为按键等效字符串

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

如何将关键代码(例如 kVK_ANSI_1)转换为可以传递给 setKeyEquivalent 的字符串(因此对于 kVK_ANSI_1,我会得到@“1”)?为什么有两种方法来指定键?如果只有一种表示形式会更有意义。

最佳答案

我最终使用了以下函数here .

/* Returns string representation of key, if it is printable.
* Ownership follows the Create Rule; that is, it is the caller's
* responsibility to release the returned object. */
CFStringRef createStringForKey(CGKeyCode keyCode)
{
TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();
CFDataRef layoutData =
TISGetInputSourceProperty(currentKeyboard,
kTISPropertyUnicodeKeyLayoutData);
const UCKeyboardLayout *keyboardLayout =
(const UCKeyboardLayout *)CFDataGetBytePtr(layoutData);

UInt32 keysDown = 0;
UniChar chars[4];
UniCharCount realLength;

UCKeyTranslate(keyboardLayout,
keyCode,
kUCKeyActionDisplay,
0,
LMGetKbdType(),
kUCKeyTranslateNoDeadKeysBit,
&keysDown,
sizeof(chars) / sizeof(chars[0]),
&realLength,
chars);
CFRelease(currentKeyboard);

return CFStringCreateWithCharacters(kCFAllocatorDefault, chars, 1);
}

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

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