【发布时间】:2019-04-28 21:20:34
【问题描述】:
我无法让 GameController 类与我的操纵杆一起工作。但是我发现了一个简单的库https://github.com/suzukiplan/gamepad-osx 使用 IOKit 编写的 C 语言,并使用以下代码为main.swift 设法让它在 swift 控制台应用程序中工作:
import Foundation
func callback(_ type: Int32, _ page: Int32, _ usage: Int32, _ value: Int32) -> Void {
print("Type: \(type); page: \(page), usage: \(usage), value: \(value)")
}
let ctx = gamepad_init(1, 1, 0)
if ctx == nil {
print("Init failed")
exit(4)
}
gamepad_set_callback(ctx, callback(_:_:_:_:))
CFRunLoopRun()
exit(0)
控制台消息:
attched device: Controller
attched device: 2.4G RX
attched device: VirtualHIDKeyboard
Type: 2; page: 7, usage: 227, value: 1
Type: 2; page: 7, usage: -1, value: 6
....
由于这将在 MacOS 应用程序中使用,我在主视图中使用以下代码创建了一个单视图 Cocoa 应用程序:
import AppKit
class MainView: NSView {
required init?(coder decoder: NSCoder) {
super.init(coder: decoder)
print("Initialising started")
let ctx = gamepad_init(1, 1, 0)
if ctx == nil {
print("Init failed")
return
}
print("Initialising succeeded")
gamepad_set_callback(ctx, callback(_:_:_:_:))
print("Callback attached")
}
}
func callback(_ type: Int32, _ page: Int32, _ usage: Int32, _ value: Int32) -> Void {
print("Type: \(type); page: \(page), usage: \(usage), value: \(value)")
}
窗口出现,但我在控制台上得到的只是:
Initialising started
Initialising succeeded
Callback attached
根本没有设备附件消息!
还在主视图和 AppDelegate 中尝试了一个正在运行的附加线程 (DispatchQueue)(这次是 CFRunLoopRun):
func applicationDidFinishLaunching(_ aNotification: Notification) {
DispatchQueue(label: "Joy").async {
print("Initialising started")
let ctx = gamepad_init(1, 1, 0)
if ctx == nil {
print("Init failed")
return
}
print("Initialising succeeded")
gamepad_set_callback(ctx, callback(_:_:_:_:))
CFRunLoopRun()
}
}
同样,没有附件消息
【问题讨论】: