Joystick 库在命令行中工作,但不能作为 macOS 应用程序
Joystick library works in comand line, but not as a macos app
我无法让 GameController class 使用我的游戏杆。但是我发现一个简单的库 https://github.com/suzukiplan/gamepad-osx 使用 IOKit 编写的是 C 并设法让它在 swift 控制台应用程序中使用以下代码 main.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)")
}
出现了window,但我在控制台上得到的只是:
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()
}
}
同样,没有附件消息
解决方案:在项目的 Capabilities
选项卡中为 App Sandbox 勾选 USB
。
感谢 https://forums.developer.apple.com/message/358820#358820 的想法。
我无法让 GameController class 使用我的游戏杆。但是我发现一个简单的库 https://github.com/suzukiplan/gamepad-osx 使用 IOKit 编写的是 C 并设法让它在 swift 控制台应用程序中使用以下代码 main.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)")
}
出现了window,但我在控制台上得到的只是:
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()
}
}
同样,没有附件消息
解决方案:在项目的 Capabilities
选项卡中为 App Sandbox 勾选 USB
。
感谢 https://forums.developer.apple.com/message/358820#358820 的想法。