如标题中所述,当执行hid程序以枚举所有隐藏设备时,它会打印Apple Internal Keyboard / Trackpad
3次。我连接了一个外部键盘,只显示了一次,为什么会这样?
码:
#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 detected\n");
else
{
printf("%lld HID devices found\n", (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
因此,当您检查路径时,它们的所有路径都不同。同样在路径中你会看到它到底是什么。