我正在学习swift,我正在尝试制作一个简单的应用程序,我可以用键盘移动一个物体(默认的船)。我正在设计一个事件来表示左边的按键。因为我不需要鼠标在特定的位置,所以NSLocation对我来说没有什么实际的用处。官方文档告诉我将变量locationInWindow作为未定义返回,这是我的NSLocation的NSPoint。
我不确定他们这样说是什么意思。我尝试使用_undefined关键字,但随后出现了以下内容:
无法将'(@autoclosure () -> String,StaticString,UInt) -> _‘类型的值转换为指定的类型'NSPoint’(也称为'CGPoint')
以下是我的代码
var locationInWindow : NSPoint{
return _undefined
}
let movingLeftEvent = NSEvent.keyEvent(with:NSEvent.EventType.keyDown, location: nil, modifierFlags:[], timestamp: [], windowNumber: 0, context: nil , characters: <#String#>, charactersIgnoringModifiers: <#String#>, isARepeat: false, keyCode: <#UInt16#>)
发布于 2018-08-05 05:56:41
您只需要通过addLocalMonitorForEvents
连接到视图控制器
NSEvent.addLocalMonitorForEvents(matching: .keyDown) {
self.keyDown(with: $0)
return $0
}
并实现自定义的keyDown方法:
override func keyDown(with event: NSEvent) {
switch event.keyCode {
case 123:
// run left arrow code action
ship.runAction(
SCNAction.repeatForever(
SCNAction.rotateBy(x: 0, y: -2, z: 0, duration: 1)
)
)
case 124:
// run right arrow code action
ship.runAction(
SCNAction.repeatForever(
SCNAction.rotateBy(x: 0, y: 2, z: 0, duration: 1)
)
)
default:
print("event.keyCode:", event.keyCode)
}
}
https://stackoverflow.com/questions/51689569
复制相似问题