gpt4 book ai didi

c++ - 为什么 HID 在 mac 中枚举 Apple Internal Keyboard/Trackpad 3 次?

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:44:36 26 4
gpt4 key购买 nike

如标题所述,当执行 hid 程序以枚举所有 hid 设备时,它会打印 3 次 Apple Internal Keyboard/Trackpad。我连接了外接键盘,但只显示了 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 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

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

关于c++ - 为什么 HID 在 mac 中枚举 Apple Internal Keyboard/Trackpad 3 次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41582281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com