以下代码可以完美地获取已连接的 HID 设备的列表:
import Foundation
import IOKit
import IOKit.usb
import IOKit.hid
private func createDeviceMatchingDictionary( usagePage: Int, usage: Int) -> CFMutableDictionary {
let dict = [
kIOHIDDeviceUsageKey: usage,
kIOHIDDeviceUsagePageKey: usagePage
] as NSDictionary
return dict.mutableCopy() as! NSMutableDictionary;
}
let manager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone));
let keyboard = createDeviceMatchingDictionary(usagePage: kHIDPage_GenericDesktop, usage: kHIDUsage_GD_Keyboard)
IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone) )
IOHIDManagerSetDeviceMatching(manager, keyboard)
let devices = IOHIDManagerCopyDevices(manager)
if (devices != nil) {
print("Found devices!")
}
else {
print("Did not find any devices :(")
}
如果我将相同的代码放入 Cocoa 应用程序中applicationDidFinishLaunching
, then devices
is nil
.
如何获取 Cocoa 应用程序中的设备列表?
为什么IOHIDManagerCopyDevices()
仅当在 Cocoa 应用程序中运行时才返回任何内容?我缺少什么?
AHA!
有一个.entitlements
文件包含在 XCode 项目中,您需要添加一行com.apple.security.device.usb
并将其设置为“YES”对于 Cocoa 项目。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)