我正在尝试使用IOHIDManager获取修饰键事件,因为Cocoa flagsChanged事件缺乏(难以区分按下/释放,左/右如果两者都关闭等)这里是我创建管理器并注册回调的代码.
IOHIDManagerRef hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
kIOHIDOptionsTypeNone);
if (CFGetTypeID(hidManager) != IOHIDManagerGetTypeID())
return 1;
CFMutableDictionaryRef capsLock =
myCreateDeviceMatchingDictionary(0x07, 0x39);
CFMutableDictionaryRef lctrl =
myCreateDeviceMatchingDictionary(0x07, 0xE0);
CFMutableDictionaryRef lshift =
myCreateDeviceMatchingDictionary(0x07, 0xE1);
CFMutableDictionaryRef lalt =
myCreateDeviceMatchingDictionary(0x07, 0xE2);
CFMutableDictionaryRef lsuper =
myCreateDeviceMatchingDictionary(0x07, 0xE3);
CFMutableDictionaryRef rctrl =
myCreateDeviceMatchingDictionary(0x07, 0xE4);
CFMutableDictionaryRef rshift =
myCreateDeviceMatchingDictionary(0x07, 0xE5);
CFMutableDictionaryRef ralt =
myCreateDeviceMatchingDictionary(0x07, 0xE6);
CFMutableDictionaryRef rsuper =
myCreateDeviceMatchingDictionary(0x07, 0xE7);
CFMutableDictionaryRef matchesList[] = {
capsLock,
lctrl,
lshift,
lalt,
lsuper,
rctrl,
rshift,
ralt,
rsuper
};
CFArrayRef matches = CFArrayCreate(kCFAllocatorDefault,
(const void **)matchesList, 9, …Run Code Online (Sandbox Code Playgroud) 我想要映射:
我已经详尽地搜索了任何可以完全自由地重新映射键盘输入的工具,但却无法找到.(Windows有AutoHotkey).
我打算编写自己的工具来解析配置文件.
但是如何实际挖掘并做到这一点?
解决这个问题需要彻底了解通过操作系统进行击键的过程,以便在适当的时候进行拦截.
我想我需要在低级别处吃掉事件,它仍然是一个虚拟密钥代码,然后提供我自己的自定义映射系统并在系统中进一步注入适当的事件.
但在哪里(以及如何)?
编辑:我在下面的答案中详述了我的研究结果(可能应该在某个时候将其迁移回问题).
我现在已经发现如何在OS X上以低级别挂机/点击键盘事件:如何在MacBook键盘上点击(挂钩)F7到F12和Power/Eject
从该答案中打印出代码:
// compile and run from the commandline with:
// clang -framework coreFoundation -framework IOKit ./HID.c -o hid
// sudo ./hid
// This code works with the IOHID library to get notified of keys.
// Still haven't figured out how to truly intercept with
// substitution.
#include <IOKit/hid/IOHIDValue.h>
#include <IOKit/hid/IOHIDManager.h>
void myHIDKeyboardCallback( void* context, IOReturn result, void* sender, IOHIDValueRef value )
{
IOHIDElementRef elem = IOHIDValueGetElement( value );
if (IOHIDElementGetUsagePage(elem) != 0x07)
return;
uint32_t scancode = IOHIDElementGetUsage( …Run Code Online (Sandbox Code Playgroud)