为什么 HID 在 mac 枚举苹果内置键盘/触控板 3 次

Why HID in mac enumerating Apple Internal Keyboard / Trackpad 3 times?

本文关键字:键盘 内置 HID mac 枚举 苹果 为什么      更新时间:2023-10-16

如标题中所述,当执行hid程序以枚举所有hid设备时,它会打印Apple Internal Keyboard / Trackpad 3次。我已经连接了外部键盘并仅显示了 1 次,这是为什么?

法典:

#include <IOKit/hid/IOHIDLib.h>
/*
 MAIN()
 ------
 */
int main(void)
{
    IOHIDManagerRef hid_manager;
    char string_buffer[1024];
    CFIndex number_of_devices;
    CFSetRef device_set;
    const IOHIDDeviceRef *device_array;
    const IOHIDDeviceRef *current;
    CFNumberRef vendor, product;
    long vendor_id, product_id;
    CFStringRef manufacturer, product_name;

    /*
     Get a handle to the HID manager
     */
    hid_manager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
    /*
     Enumerate all HID devices and count how many we have.
     */
    IOHIDManagerSetDeviceMatching(hid_manager, NULL);
    IOHIDManagerOpen(hid_manager, kIOHIDOptionsTypeNone);
    device_set = IOHIDManagerCopyDevices(hid_manager);
    number_of_devices = device_set == NULL ? 0 : CFSetGetCount(device_set);
    /*
     Tell the user how many we found
     */
    if (number_of_devices == 0)
        printf("No HID devices detectedn");
    else
    {
        printf("%lld HID devices foundn", (long long)number_of_devices);
        /*
         Get the list into a C++ array
         */
        device_array = new IOHIDDeviceRef [number_of_devices];
        CFSetGetValues(device_set, (const void **)device_array);
        /*
         Iterate the device list
         */
        for (current = device_array; current < device_array + number_of_devices; current++)
        {
            vendor_id = product_id = 0;
            /*
             Get the vendor ID (which is a 32-bit integer)
             */
            if ((vendor = (CFNumberRef)IOHIDDeviceGetProperty(*current, CFSTR(kIOHIDVendorIDKey))) != NULL)
                CFNumberGetValue(vendor, kCFNumberSInt32Type, &vendor_id);
            printf("VID:%04lX ", vendor_id);
            /*
             Get the product ID (which is a 32-bit integer)
             */
            if ((product = (CFNumberRef)IOHIDDeviceGetProperty(*current, CFSTR(kIOHIDProductIDKey))) != NULL)
                CFNumberGetValue((CFNumberRef)product, kCFNumberSInt32Type, &product_id);
            printf("PID:%04lX ", product_id);
            /*
             Get the manufacturer name (which is a string)
             */
            if ((manufacturer = (CFStringRef)IOHIDDeviceGetProperty(*current, CFSTR(kIOHIDManufacturerKey)))!= NULL)
            {
                CFStringGetCString(manufacturer, string_buffer, sizeof(string_buffer), kCFStringEncodingUTF8);
                printf("%s ", string_buffer);
            }
            /*
             Get the product name (which is a string)
             */
            if ((product_name = (CFStringRef)IOHIDDeviceGetProperty(*current, CFSTR(kIOHIDProductKey))) != NULL)
            {
                CFStringGetCString(product_name, string_buffer, sizeof(string_buffer), kCFStringEncodingUTF8);
                printf("(%s)", string_buffer);
            }
            puts("");
        }
        /*
         We're finished with the device set and device list so free them.
         */
        CFRelease(device_set);
        delete [] device_array;
    }
    return 0;
}

输出:

4 HID devices found
VID:413C PID:2107 Dell (Dell USB Entry Keyboard)
VID:05AC PID:0259 Apple Inc. (Apple Internal Keyboard / Trackpad)
VID:05AC PID:0259 Apple Inc. (Apple Internal Keyboard / Trackpad)
VID:05AC PID:0259 Apple Inc. (Apple Internal Keyboard / Trackpad)

当您检查所有设备的路径时,您将看到差异。

Device Found type: 05ac 0262 path: IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS12@14400000/Apple Internal Keyboard / Trackpad@14400000/Apple Internal Keyboard@0/AppleUSBTCKeyboard@14400000,0 serial_number: Manufacturer: Apple Inc. Product: Apple Internal Keyboard / Trackpad Release: 225 Interface: -1

Device Found type: 05ac 0262 path: IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS12@14400000/Apple Internal Keyboard / Trackpad@14400000/Touchpad@2/AppleUSBTCButtons@14400000,2 serial_number: Manufacturer: Apple Inc. Product: Apple Internal Keyboard / Trackpad Release: 225 Interface: -1

Device Found type: 05ac 0262 path: IOService:/AppleACPIPlatformExpert/PCI0@0/AppleACPIPCI/XHC1@14/XHC1@14000000/HS12@14400000/Apple Internal Keyboard / Trackpad@14400000/Touchpad@1/AppleUSBMultitouchDriver@14400000,1 serial_number: Manufacturer: Apple Inc. Product: Apple Internal Keyboard / Trackpad Release: 225 Interface: -1

因此,当您检查路径时,它们都不同。同样在路径中,您将在最后看到它的用途。