How to tap/hook keyboard events in OSX and record which keyboard fires each event

前端 未结 2 1013
轻奢々
轻奢々 2021-01-18 09:13

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

2条回答
  •  孤街浪徒
    2021-01-18 09:35

    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.

提交回复
热议问题