相关疑难解决方法(0)

如何将ASCII字符转换为CGKeyCode?

我需要一个函数,给定一个字符,返回CGKeyCode与当前键盘布局上该字符的位置相关联的函数.例如,给定"b",kVK_ANSI_B如果使用US QWERTY,或者kVK_ANSI_N使用Dvorak ,它应该返回.

Win32 API具有VkKeyScan()用于此目的的功能; X11具有此功能XStringToKeySym().CG API中有这样的功能吗?

我需要这个来传递参数CGEventCreateKeyboardEvent().我尝试使用CGEventKeyboardSetUnicodeString(),但显然不支持修饰符标志(我需要).

我已经广泛搜索了这个但是找不到合适的答案.目前我正在使用以下代码(在线发现),它有效,但不是很优雅(而且很难解释如何简化),我宁愿不在生产代码中使用它:

#include <stdint.h>
#include <stdio.h>
#include <ApplicationServices/ApplicationServices.h>

CGKeyCode keyCodeForCharWithLayout(const char c,
                                   const UCKeyboardLayout *uchrHeader);

CGKeyCode keyCodeForChar(const char c)
{
    CFDataRef currentLayoutData;
    TISInputSourceRef currentKeyboard = TISCopyCurrentKeyboardInputSource();

    if (currentKeyboard == NULL) {
        fputs("Could not find keyboard layout\n", stderr);
        return UINT16_MAX;
    }

    currentLayoutData = TISGetInputSourceProperty(currentKeyboard,
                                                kTISPropertyUnicodeKeyLayoutData);
    CFRelease(currentKeyboard);
    if (currentLayoutData == NULL) {
        fputs("Could not find layout data\n", …
Run Code Online (Sandbox Code Playgroud)

c macos keycode keypress quartz-graphics

29
推荐指数
5
解决办法
2万
查看次数

标签 统计

c ×1

keycode ×1

keypress ×1

macos ×1

quartz-graphics ×1