macos – Swift OSX关键事件

前端之家收集整理的这篇文章主要介绍了macos – Swift OSX关键事件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在使用 swift中的 Cocoa OSX项目,需要使用键盘输入来执行操作.在keydown上我想在窗口上移动一个对象,但是一旦键松开就停止对象.我查看了AppKit的文档并找到了KeyDown函数,但我似乎无法弄清楚如何使用它.我想在我的游戏更新计时器中创建一个函数调用它.谢谢
  1. import Cocoa
  2. import Appkit
  3.  
  4. @NSApplicationMain
  5. class AppDelegate: NSObject,NSApplicationDelegate {
  6.  
  7. @IBOutlet weak var window: NSWindow!
  8.  
  9. func applicationDidFinishLaunching(aNotification: NSNotification) {
  10. // Insert code here to initialize your application
  11.  
  12. func keyDown(theEvent: NSEvent) {
  13. if (theEvent.keyCode == 1){
  14. println("test")
  15. }
  16.  
  17. }
  18.  
  19. }
  20.  
  21. func applicationWillTerminate(aNotification: NSNotification) {
  22. // Insert code here to tear down your application
  23.  
  24.  
  25.  
  26. }

}

这是一些示例代码
  1. override func keyDown(theEvent: NSEvent) {
  2. if (theEvent.keyCode == 1){
  3. //do whatever when the s key is pressed
  4. }
  5.  
  6. }

关键代码

猜你在找的Swift相关文章