解决了!
在本指南的帮助下: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;
}