macos - 有没有办法确定 Mac 是否连接了多个鼠标?

标签 macos cocoa mouse

我正在寻找一种方法来确定运行我的游戏的 Mac 是否连接了多个鼠标。典型的现实示例是连接了外部鼠标的 MacBook(带有内置触控板)。

我的游戏对常见配置有不同的控制设置,例如键盘+鼠标、仅键盘(例如,只有触控板,没有鼠标的 MacBook)和游戏 handle 。理想情况下,我能够在游戏中检测到这一点并相应地设置控件。

我计划支持 Mac OS 10.7+。

我可以使用 Cocoa(或非 Cocoa)API 来获取此信息吗?

作为引用,我已经询问(并得到了答案)similar question适用于基于 Windows 的计算机。

最佳答案

您应该处理 IOKit...以下示例枚举了连接到系统的所有 USB 设备,您可以使用它来查看是否连接了指点设备:

#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>

int main(int argc, const char *argv[])
{
    CFMutableDictionaryRef matchingDict;
    io_iterator_t iter;
    kern_return_t kr;
    io_service 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)))
    {
        /* do something with device, eg. check properties */
        /* ... */
        /* And free the reference taken before continuing to the next item */
        IOObjectRelease(device);
    }

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

内部触控板应被视为连接的 USB 设备,但我不确定......

关于macos - 有没有办法确定 Mac 是否连接了多个鼠标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23017879/

相关文章:

c++ - Win32 : How to trap left and right mouse button clicked at the same time

php - 命令行和 apache 中的不同版本的 php

xcode - 如何在 macOS Swift 中保存 NSTextField 内容?

objective-c - NS 大纲 View : Expand all objects of a specific class only

cocoa - 允许特定数量的 NSWindow 副本可见

cocoa - 基于 View 的 NSTableView 和模糊 float 组行

testing - 你会如何测试鼠标?描述尽可能多的测试用例?

eclipse - 在 OSX 上启动 eclipse luna (SR1) 的异常

swift - 沙盒扩展创建失败 : client lacks entitlements? 路径:

delphi - 如何在Delphi中检测前进和后退鼠标按钮事件?