7

我正在将游戏从 pc 移植到 osx,但我遇到了输入事件。主游戏窗口是一个与 C++ 后端交互的可可应用程序。pc 代码使用 DirectInput 和 windows 消息来生成游戏可以理解的键盘和鼠标事件。

当我第一次启动端口时,我用 Carbon 事件处理替换了 windows 消息,但后来发现 Cocoa 应用程序不会触发 carbon 事件。我做了更多的阅读,发现了 HIDManager,它似乎可以做我想做的事情,并且可以从 c++ 访问。使用此处的帖子中的信息使用 IOHIDManager 获取修饰键事件我设法使键盘输入正常工作,但到目前为止还无法扩展示例代码以生成鼠标事件。代码如下:

 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。

4

0 回答 0