OsX中的USB HID API回调永远不会被游戏手柄或其他任何工具调用

用户名

我试图在游戏中检测并使用连接到Mac的游戏手柄。我为此使用IOKit,但没有成功。我下载了可查看所有附带的游戏手柄的Mac应用程序后,该游戏手柄被认可。它是Logitech F310,我将其设置为DirectInput。我在awakeFromNib内部调用setupInput,但从未调用过任何回调。没有引发错误或异常。我不知道为什么不调用回调。我究竟做错了什么?我在想,也许这是运行循环的问题。我尝试了CFRunLoopGetMain和CFRunLoopGetCurrent。

不知道该怎么办。

编辑:根据某人的建议,我试图将相同的GamePad代码放入一个新项目中,并且该代码可以正常工作。类的结构有些不同,但是即使我尝试复制这些类,也无法在我的应用程序中使它正常工作。我的应用程序使用AppDelegate和NSOpenGLView。奇怪的是,它没有NSViewController。最小的应用程序使用NSOpenGLView,但也使用NSResponder,当我将游戏手柄代码放入(自定义)NSResponder类中时,它将“运行”(检测游戏手柄并响应输入)。我试图将自定义类添加到我的应用程序中,但“无法正常工作”。我想念什么?

void gamepadWasAdded(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef device) {
    NSLog(@"Gamepad was plugged in");
}

void gamepadWasRemoved(void* inContext, IOReturn inResult, void* inSender, IOHIDDeviceRef device) {
    NSLog(@"Gamepad was unplugged");
}

void gamepadAction(void* inContext, IOReturn inResult, void* inSender, IOHIDValueRef value) {
    NSLog(@"Gamepad talked!");
    IOHIDElementRef element = IOHIDValueGetElement(value);
    NSLog(@"Element: %@", element);
    int elementValue = IOHIDValueGetIntegerValue(value);
    NSLog(@"Element value: %i", elementValue);
}

-(void) setupInput {
    //get a HID manager reference
    hidManager = IOHIDManagerCreate(kCFAllocatorDefault,
                                                    kIOHIDOptionsTypeNone);

    //define the device to search for, via usage page and usage key
    NSMutableDictionary* criterion = [[NSMutableDictionary alloc] init];
    [criterion setObject: [NSNumber numberWithInt: kHIDPage_GenericDesktop]
                  forKey: (NSString*)CFSTR(kIOHIDDeviceUsagePageKey)];
    [criterion setObject: [NSNumber numberWithInt: kHIDUsage_GD_Joystick]
                  forKey: (NSString*)CFSTR(kIOHIDDeviceUsageKey)];

    //search for the device
    IOHIDManagerSetDeviceMatching(hidManager,
                                  (__bridge CFDictionaryRef)criterion);

    //register our callback functions
    IOHIDManagerRegisterDeviceMatchingCallback(hidManager, gamepadWasAdded,
                                               (__bridge void*)self);
    IOHIDManagerRegisterDeviceRemovalCallback(hidManager, gamepadWasRemoved,
                                              (__bridge void*)self);
    IOHIDManagerRegisterInputValueCallback(hidManager, gamepadAction,
                                           (__bridge void*)self);

    //scedule our HIDManager with the current run loop, so that we
    //are able to recieve events from the hardware.
    IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetMain(),
                                    kCFRunLoopDefaultMode);

    //open the HID manager, so that it can start routing events
    //to our callbacks.
    IOHIDManagerOpen(hidManager, kIOHIDOptionsTypeNone);

}

// Put our timer in -awakeFromNib, so it can start up right from the beginning
-(void)awakeFromNib
{
    renderTimer = [NSTimer timerWithTimeInterval:0.001   //a 1ms time interval
                                          target:self
                                        selector:@selector(timerFired:)
                                        userInfo:nil
                                         repeats:YES];

    [[NSRunLoop currentRunLoop] addTimer:renderTimer
                                 forMode:NSDefaultRunLoopMode];
    [[NSRunLoop currentRunLoop] addTimer:renderTimer
                                 forMode:NSEventTrackingRunLoopMode]; //Ensure timer fires during resize
    [self setupInput];
}
用户名

好的,我发现了问题。问题是我的Mac应用程序在沙箱中,而我没有选中“硬件/ USB”复选框。选中此选项,它将与原始代码一起使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章