I\'ve now discovered how to hook/tap keyboard events on OS X at a low level: How to tap (hook) F7 through F12 and Power/Eject on a MacBook keyboard
Printing out the
I was working on this problem, and finally got the solution. OP's code is correct, if you'd like the product ID of the keyboard/pad, add lines to the myHIDKeyboardCallback()
function:
void myHIDKeyboardCallback(void* context, IOReturn result, void* sender, IOHIDValueRef value){
IOHIDElementRef elem = IOHIDValueGetElement(value);
if (IOHIDElementGetUsagePage(elem) != 0x07)
return;
IOHIDDeviceRef device = sender;
int32_t pid = 1;
CFNumberGetValue(IOHIDDeviceGetProperty(device, CFSTR("idProduct")), kCFNumberSInt32Type, &pid);
uint32_t scancode = IOHIDElementGetUsage(elem);
if (scancode < 4 || scancode > 231)
return;
long pressed = IOHIDValueGetIntegerValue(value);
printf("scancode: %d, pressed: %ld, keyboardId=%d\n", scancode, pressed, pid);
}
As @pmdj said you can use IOHIDDeviceRegisterInputValueCallback()
, I was having trouble with this, and found that sender
argument provided keyboard product id anyways.