如何在MAC OS中获取有关鼠标,键盘,游戏手柄的信息

时间:2013-06-26 03:58:33

标签: macos input device

需要有关设备按钮,数量等的信息

我试过iokit,但得到了一个奇怪的清单

name = IOUSBRootHubDevice 
name = IOUSBHubDevice 
name = IOUSBDevice 
name = IOUSBHubDevice 
name = IOUSBDevice 
name = IOUSBRootHubDevice 
name = IOUSBHubDevice 
name = IOUSBDevice 
name = IOUSBDevice 
name = IOUSBDevice 

我的代码

CFMutableDictionaryRef matchingDict;
                    io_iterator_t iter;
                    kern_return_t kr;
                    io_service_t device;
                    io_name_t name;
                    //io_object_t device;

                    /* set up a matching dictionary for the class */
                    matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
                    if (matchingDict == NULL){
                        return -1; // fail
                    }
                    /* Now we have a dictionary, get an iterator.*/
                    kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
                    if (kr != KERN_SUCCESS){
                        return -1;
                    }

                    /* iterate */
                    while ((device = IOIteratorNext(iter))){

                        IOObjectGetClass(device, name);

                        printf("name = %s \n", name);
                        /* do something with device, eg. check properties */
                        /* ... */
                        /* And free the reference taken before continuing to the next item */
                        IOObjectRelease(device);
                    }

                    /* Done, release the iterator */
                    IOObjectRelease(iter);

可能有更好的选择,但我在谷歌找不到

1 个答案:

答案 0 :(得分:0)

让自己陷入困境,因为它如此

static void MyCreateHIDDeviceInterface(io_object_t hidDevice,
                                   IOHIDDeviceInterface122 ***hidDeviceInterface)
{
io_name_t               className;
IOCFPlugInInterface     **plugInInterface = NULL;
HRESULT                 plugInResult = S_OK;
SInt32                  score = 0;
IOReturn                ioReturnValue = kIOReturnSuccess;

ioReturnValue = IOObjectGetClass(hidDevice, className);

printf("Failed to get class name.\n");//print_errmsg_if_io_err(ioReturnValue, "Failed to get class name.");

printf("Found device type %s\n", className);

ioReturnValue = IOCreatePlugInInterfaceForService(hidDevice,
                                                  kIOHIDDeviceUserClientTypeID,
                                                  kIOCFPlugInInterfaceID,
                                                  &plugInInterface,
                                                  &score);

if (ioReturnValue == kIOReturnSuccess)
{
    //Call a method of the intermediate plug-in to create the device
    //interface
    plugInResult = (*plugInInterface)->QueryInterface(plugInInterface,
                                                      CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID),
                                                      (LPVOID *) hidDeviceInterface);
    printf("Couldn't create HID class device interface.\n");//print_errmsg_if_err(plugInResult != S_OK, "Couldn't create HID class device interface");
    IODestroyPlugInInterface(plugInInterface);
    //(*plugInInterface)->Release(plugInInterface);
}
}


void getCookies(IOHIDDeviceInterface122** deviceInterface){

CFArrayRef          elements;
CFDictionaryRef     element;
CFTypeRef           object;
long                number;
long                usagePage;
long                usage;
IOHIDElementCookie  cookie;


(*deviceInterface)->copyMatchingElements(deviceInterface, NULL, &elements);
for ( CFIndex i=0; i<CFArrayGetCount(elements); i++ ){
    element = (CFDictionaryRef)CFArrayGetValueAtIndex(elements, i);

    // Get usage page
    object = CFDictionaryGetValue(element, CFSTR(kIOHIDElementUsagePageKey));
    if( object==0 || CFGetTypeID(object) != CFNumberGetTypeID() ){
        continue;
    }
    if( !CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, &number) ){
        continue;
    }
    usagePage = number;
    if( usagePage!=kHIDPage_GenericDesktop && usagePage!=kHIDPage_Button ){
        continue;
    }

    // Get usage
    object = CFDictionaryGetValue( element, CFSTR(kIOHIDElementUsageKey) );
    if( object==0 || CFGetTypeID(object) != CFNumberGetTypeID() ){
        continue;
    }
    if( !CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, &number) ){
        continue;
    }
    usage = number;

    // Get cookie
    object = CFDictionaryGetValue( element, CFSTR(kIOHIDElementCookieKey) );
    if( object==0 || CFGetTypeID(object) != CFNumberGetTypeID() ){
        continue;
    }
    if( !CFNumberGetValue((CFNumberRef) object, kCFNumberLongType, &number) ){
        continue;
    }
    cookie = (IOHIDElementCookie) number;

    if(usagePage == kHIDPage_GenericDesktop){
        switch( usage )
        {
            case kHIDUsage_GD_Pointer: {
                printf("kHIDUsage_GD_Pointer \n");
                break;
            }
            case kHIDUsage_GD_Mouse:  {
                printf("kHIDUsage_GD_Mouse \n");
                break;
            };
            case kHIDUsage_GD_Joystick: {
                printf("kHIDUsage_GD_Joystick \n");
                break;
            }
            case kHIDUsage_GD_GamePad: {

                printf("kHIDUsage_GD_GamePad \n");
                break;
            }
            case kHIDUsage_GD_Keyboard:{

                printf("kHIDUsage_GD_Keyboard \n");
                break;
            };
            case kHIDUsage_GD_Keypad: {

                printf("kHIDUsage_GD_Keypad \n");
                break;
            };
            case kHIDUsage_GD_MultiAxisController:{

                printf("kHIDUsage_GD_MultiAxisController \n");
                break;
            };
            case kHIDUsage_GD_X: {

                printf("kHIDUsage_GD_X \n");
                break;
            };
            case kHIDUsage_GD_Y:  {
                printf("kHIDUsage_GD_Y \n");
                break;
            };
        }
    } else if( usagePage == kHIDPage_Button){
        printf("kHIDPage_Button \n");
    }
}
}

int main(int argc, const char * argv[])
{
CFMutableDictionaryRef matchingDict;
io_iterator_t iter;
kern_return_t kr;
io_object_t device;
char name[128];


IOHIDDeviceInterface122 **deviceInterface = NULL;

/* set up a matching dictionary for the class */
matchingDict = IOServiceMatching(kIOHIDDeviceKey);//kIOHIDDeviceKey
if (matchingDict == NULL)
{
    return -1; // fail
}

/* Now we have a dictionary, get an iterator.*/
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr != KERN_SUCCESS)
{
    return -1;
}

/* iterate */
while ((device = IOIteratorNext(iter)))
{
    IOObjectGetClass(device, name);
    if(!strcmp(name,"BNBMouseDevice")){
        MyCreateHIDDeviceInterface(device, &deviceInterface);
        if(*deviceInterface != NULL){
            getCookies(deviceInterface);
            (*deviceInterface)->Release(deviceInterface);
        }
        printf("name = %s \n \n", name);
    }else if(!strcmp(name,"AppleBluetoothHIDKeyboard")){
        MyCreateHIDDeviceInterface(device, &deviceInterface);
        if(*deviceInterface != NULL){
            getCookies(deviceInterface);
            (*deviceInterface)->Release(deviceInterface);
        }
        printf("name = %s \n \n", name);
    }else if(!strcmp(name,"IOUSBHIDDriver")){
        MyCreateHIDDeviceInterface(device, &deviceInterface);
        if(*deviceInterface != NULL){
            getCookies(deviceInterface);
            (*deviceInterface)->Release(deviceInterface);
        }
        printf("name = %s \n \n", name);
    }
    IOObjectRelease(device);
}

/* Done, release the iterator */
IOObjectRelease(iter);
return 0;
}