【问题标题】:OSX: read raw keyboard input in COSX:读取 C 中的原始键盘输入
【发布时间】:2015-01-28 21:54:12
【问题描述】:

我正在修改廉价的图形输入板。平板电脑上的热键不能按我的意愿工作,我想修改它们。

即:其中一个热键输出“+”(加号),另一个输出“-”(减号)等。

我的目标是读取特定“键盘”的输入(不是全局用户输入),检查值并发布自定义 CGEventCreateKeyboardEvent。

我成功让平板电脑通过 CGEventCreateMouseEvent 控制鼠标移动。

到目前为止我的理论:

1。使用 hidapi 访问键盘

  • 直接从键盘读取
  • 检查十六进制值
  • 做一些 c 代码
  • 这里的问题:原始隐藏输出中不存在热键

2。访问全局用户输入

  • 读取用户级别的任何输入
  • 检查输入源(即:mac-keyboard、cheap-keyboard)
  • 做一些 c 代码

【问题讨论】:

    标签: c macos input keyboard driver


    【解决方案1】:

    解决了! 在本指南的帮助下:https://github.com/sdegutis/mjolnir/issues/9

    基本上你需要做的是:

    • 在 RunLoop 中创建一个 EventTap 并为该事件创建一个后备函数
    • 在您的普通键盘和您想要破解的键盘上输入一些内容
    • 您应该会看到每个键盘的唯一编号
    • 将此值用于您的条件
    MY_DEBUGGED_KEYBOARD 44
    int keyboard = 0;
    
    CGEventRef
    myCGEventCallback(CGEventTapProxy proxy, CGEventType type,
        CGEventRef event, void *refcon) {
    // Paranoid sanity check.
    if ((type != kCGEventKeyDown) && (type != kCGEventKeyUp))
        return event;
    
    keyboard = CGEventGetIntegerValueField(event, kCGKeyboardEventKeyboardType);
    
    // if you found your keyboard-value...
    if (keyboard != MY_DEBUGGED_KEYBOARD) {
        return event;
    }
    
    
    // ... you can proceed with your stuff... i.e. remap input, etc.
    
    printf("%d\n", keyboard);
    
    
    // Set the modified keycode field in the event.
    CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, (int64_t) keycode);
    
    // We must return the event for it to be useful.
    return event;
    }
    
    int main(int argc, char* argv[]) {
    CFMachPortRef      eventTap;
    CGEventMask        eventMask;
    CFRunLoopSourceRef runLoopSource;
    
    // Create an event tap. We are interested in key presses.
    eventMask = ((1 << kCGEventKeyDown) | (1 << kCGEventKeyUp));
    eventTap = CGEventTapCreate(kCGSessionEventTap, kCGHeadInsertEventTap, 0,
            eventMask, myCGEventCallback, NULL);
    if (!eventTap) {
        fprintf(stderr, "failed to create event tap\n");
        exit(1);
    }
    
    // Create a run loop source.
    runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
    
    // Add to the current run loop.
    CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
    
    // Enable the event tap.
    CGEventTapEnable(eventTap, true);
    
    // Set it all running.
    CFRunLoopRun();
    return 0;
    }
    

    【讨论】:

      猜你喜欢
      • 2014-05-28
      • 1970-01-01
      • 2022-11-25
      • 2014-06-11
      • 1970-01-01
      • 1970-01-01
      • 2011-06-16
      • 2010-12-21
      相关资源
      最近更新 更多