我一直在使用Swift开发一个Cocoa OSX项目,需要使用键盘输入来执行操作。在按下键时,我想将一个对象移动到窗口中,但是当松开键时,停止该对象的移动。我查阅了AppKit文档,找到了KeyDown函数,但似乎无法弄清楚如何使用它。我想创建一个函数,在我的游戏更新计时器中调用,以执行此操作。谢谢
import Cocoa
import Appkit
@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet weak var window: NSWindow!
func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application
func keyDown(theEvent: NSEvent) {
if (theEvent.keyCode == 1){
println("test")
}
}
}
func applicationWillTerminate(aNotification: NSNotification) {
// Insert code here to tear down your application
}
}