使用C ++从OSX中的HIDManager获取鼠标事件

时间:2012-08-09 15:57:19

标签: c++ macos hid

我正在将游戏从PC移植到osx,而我却陷入输入事件的困境。主游戏窗口是一个与C ++后端接口的可可应用程序。 pc代码使用DirectInput和Windows消息来生成游戏理解的键盘和鼠标事件。

当我第一次启动端口时,我用Carbon事件处理替换了windows消息,但是因为发现Cocoa应用程序不会触发碳事件。我做了一些阅读并发现了HIDManager似乎做了我想要的,可以从c ++访问。使用此处帖子中的信息Using IOHIDManager to Get Modifier Key Events我设法让键盘输入工作,但到目前为止还无法扩展示例代码以生成鼠标事件。代码如下:

 void myHIDCallback(void* context, IOReturn result, void* sender, IOHIDValueRef value)
{
    IOHIDElementRef elem = IOHIDValueGetElement(value);
    if (IOHIDElementGetUsagePage(elem) == 0x07)
    {


        // Keyboard events
        card32 scancode = IOHIDElementGetUsage(elem);
        if (scancode >= 4 && scancode <= 231)
        {
            long pressed = IOHIDValueGetIntegerValue(value);

            KEY_EVENT_DETAILS details = { KEY_EVENT_NONE, NUM_KEYS };

            for (card32 n=0; n<NUM_KEYS; ++n)
            {
                if (n_direct_input_mappings[n].direct_input_key == scancode)
                {
                    details.key_type = n_direct_input_mappings[n].key;
                    break;
                }
            }


            switch (pressed)
            {
                case 0:
                    details.event_type = KEY_EVENT_KEY_DOWN;
                    break;
                case 1:
                    details.event_type = KEY_EVENT_KEY_UP;
                    break;    
            } 

            sApplication->handle_key_event(details);
        }
    }
    else                                                                                                                                            
    if (IOHIDElementGetUsagePage(elem) == 0x02)
    {
        // Mouse events
        card32 usage = IOHIDElementGetUsage(elem);
        long pressed = IOHIDValueGetIntegerValue(value);

    }
    else
    {
    }
}

CFMutableDictionaryRef myCreateDeviceMatchingDictionary(UInt32 usagePage, UInt32 usage)
{
    CFMutableDictionaryRef ret = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    if (ret)
    {
        CFNumberRef pageNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usagePage);
        if (pageNumberRef)
        {
            CFDictionarySetValue(ret, CFSTR(kIOHIDDeviceUsagePageKey), pageNumberRef);
            CFRelease(pageNumberRef);

            CFNumberRef usageNumberRef = CFNumberCreate(kCFAllocatorDefault, kCFNumberIntType, &usage);
            if (usageNumberRef)
            {
                CFDictionarySetValue(ret, CFSTR(kIOHIDDeviceUsageKey), usageNumberRef);
                CFRelease(usageNumberRef);

                return ret;

            }                
        }
        CFRelease(ret);
    }
    return NULL;
}

bool acquire_hardware_controllers(CA::Application& app, CAWindow& window_handle)
{        
    sApplication = &app;

    // Setup the keyboard event handler
    sHIDManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
    IOHIDManagerOpen(sHIDManager, kIOHIDOptionsTypeNone);

    CFMutableDictionaryRef mouse    = myCreateDeviceMatchingDictionary(0x01, 2);
    CFMutableDictionaryRef keyboard = myCreateDeviceMatchingDictionary(0x01, 6);
    CFMutableDictionaryRef keypad   = myCreateDeviceMatchingDictionary(0x01, 7);

    CFMutableDictionaryRef matchesList[] = {
        keyboard,
        keypad,
        mouse
    };

    CFArrayRef matches = CFArrayCreate(kCFAllocatorDefault, (const void**)matchesList, 3, NULL);
    IOHIDManagerSetDeviceMatchingMultiple(sHIDManager, matches);
    IOHIDManagerRegisterInputValueCallback(sHIDManager, myHIDCallback, NULL);
    IOHIDManagerScheduleWithRunLoop(sHIDManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);

}

如果有人能帮助我,那就太棒了。如果我不能使这个工作,那么我想我可以转移到Cocoa事件处理,但pc代码也有一些我希望实现的键盘和鼠标轮询,我不确定Cocoa支持轮询键盘状态 - 虽然我很乐意纠正。

TIA。

0 个答案:

没有答案