【发布时间】:2015-01-18 17:20:34
【问题描述】:
我一直在快速开发一个 Cocoa OSX 项目,该项目需要使用键盘输入来执行操作。在 keydown 上,我想在窗口中移动一个对象,但只要松开键就停止该对象。我查看了 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
}
}
【问题讨论】:
-
阅读 Apple 指南,尤其是这个developer.apple.com/library/mac/documentation/Cocoa/Conceptual/…“处理关键事件”。您还将在其中找到示例代码。
-
您不应该在该课程中使用该事件。在你的窗口类中使用它。
-
@Kendel 抱歉,但如果我只有 AppDelegate,没有 windows 类(它是一个菜单栏应用程序),我可以将覆盖函数放在哪里?