我正在玩一个廉价图形输入板的修改。平板电脑上的热键无法按我想要的方式工作,我想修改它们。
即:其中一个热键输出“+”(加号),另一个热键输出“-”(减号),等等。
我的目标是读取特定“键盘”的输入(而不是全局用户输入),检查值并发布自定义 CGEventCreateKeyboardEvent。
我成功让平板电脑通过CGEventCreateMouseEvent控制鼠标移动。
到目前为止我的理论:
1。使用hidapi访问键盘
- 直接从键盘读取
- 检查十六进制值
- 编写一些 C 代码
- 此处存在问题:原始 hid 输出中不存在热键
2。访问全局用户输入
- 读取用户级别的任何输入
- 检查输入的来源(即:mac-keyboard、cheap-keyboard)
- 编写一些 C 代码
最佳答案
解决了! 在本指南的帮助下: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;
}
关于c - 操作系统: read raw keyboard input in C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27219146/