我可以得到这样的隐藏设备
import ForceFeedback
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 trackpad = createDeviceMatchingDictionary(usagePage: kHIDPage_GenericDesktop, usage: kHIDUsage_GD_Mouse)
IOHIDManagerOpen(manager, IOOptionBits(kIOHIDOptionsTypeNone) )
IOHIDManagerSetDeviceMatching(manager, trackpad)
let devices = IOHIDManagerCopyDevices(manager)
然而,这些都不会返回 io_service_t
或 UnsafeMutablePointer<FFDeviceObjectReference>
需要使用 FFCreateDevice()
如何获得力反馈设备并使用 FFCreateEffect
在上面吗?
最佳答案
很多 Objective-C 或 C 风格的框架背后的想法是,你为你希望由函数创建的对象传入指针,函数返回代码,以便你知道操作是否成功.
您可以使用 IOHIDDeviceGetService(device)
获取您的 io_service_t
。然后,您可以创建类型为 FFDeviceObjectReference
的可选对象,并将其引用传递给 FFCreateDevice
。这是一个(未经测试的)示例,它似乎应该有效:
import IOKit.hid
import ForceFeedback
class ForceFeedback {
public func listen() {
let hidManager = IOHIDManagerCreate(kCFAllocatorDefault, IOOptionBits(kIOHIDOptionsTypeNone))
let hidDevices = [
kIOHIDDeviceUsagePageKey: kHIDPage_GenericDesktop,
kIOHIDDeviceUsageKey: kHIDUsage_GD_Mouse
]
IOHIDManagerSetDeviceMatching(hidManager, hidDevices as CFDictionary)
let deviceMatchingCallback: IOHIDDeviceCallback = {context, result, sender, device in
var ffDevice: FFDeviceObjectReference? = nil
let result = FFCreateDevice(IOHIDDeviceGetService(device), &ffDevice)
if result != FF_OK {
print("could not create force feedback device")
}
var ffEffect: FFEffectObjectReference? = nil
var effectDefinition = FFEFFECT() // You'd create your effect here
// Pick your UUID and build it here. For example, this is the constant force effect type
// E559C460-C5CD-11D6-8A1C-00039353BD00
let constantForce: CFUUID = CFUUIDCreateWithBytes(kCFAllocatorDefault,
0xE5, 0x59, 0xC4, 0x60, 0xC5, 0xCD, 0x11, 0xD6,
0x8A, 0x1C, 0x00, 0x03, 0x93, 0x53, 0xBD, 0x00)
let effectResult = FFDeviceCreateEffect(ffDevice, constantForce, &effectDefinition, &ffEffect)
if effectResult != FF_OK {
print("could not create effect")
}
// Start and stop your effect
// Don't forget to clean up your ffDevice and ffEffect when done.
}
// bridge your context and send it as a parameter instead of nil if needed
IOHIDManagerRegisterDeviceMatchingCallback(hidManager, deviceMatchingCallback, nil)
IOHIDManagerScheduleWithRunLoop(hidManager, CFRunLoopGetCurrent(), CFRunLoopMode.defaultMode.rawValue)
CFRunLoopRun()
}
}
请注意,我注册了 HID 回调,以便代码在添加设备时运行。
关于swift - 在 Swift 中获取 ForceFeedback 设备,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49929448/