Swift OSX键事件 [英] Swift OSX key event
本文介绍了Swift OSX键事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在开发一个Cocoa OSX项目在swift,需要使用键盘输入预制一个动作。在keydown我想移动一个对象在窗口,但停止对象一旦密钥被释放。我已经查看了AppKit的文档,并发现KeyDown函数,但我似乎不知道如何使用它。我想创建一个函数来调用我的游戏更新计时器,这将预先做到这一点。感谢
import Cocoa
import Appkit
@NSApplicationMain
class AppDelegate: NSObject,NSApplicationDelegate {
@IBOutlet弱var窗口:NSWindow!
func applicationDidFinishLaunching(aNotification:NSNotification){
//在此插入代码以初始化应用程序
func keyDown(theEvent:NSEvent){
if (theEvent.keyCode == 1){
println(test)
}
}
}
func applicationWillTerminate(aNotification:NSNotification){
//在此插入代码以清除应用程序
}
}
解决方案
override func keyDown(theEvent:NSEvent){
if(theEvent.keyCode == 1){
//按下s键时执行任何操作
}
}
$ b b键码:
>I have been working on a Cocoa OSX project in swift that requires the use of keyboard input to preform an action. On keydown i want to move an object across the window, but stop the object as soon as the key is let up. I have looked in the documentation for AppKit and found the KeyDown function but I cannot seem to figure out how to use it. I want to create a function to call in my game update timer that will preform this. Thanks
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 }
}
解决方案Here is some example code:
override func keyDown(theEvent: NSEvent) { if (theEvent.keyCode == 1){ //do whatever when the s key is pressed } }
Key codes:
这篇关于Swift OSX键事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文