在xcode最后一个版本中,我试图获取NSTextField的keyDown事件.然而,尽管在互联网上有多个教程(代表,控制器......),我仍然无法接收它.
对我来说有什么简单的暗示吗?
谢谢 !
我有很多 NSTextFields,我想知道,如果用户在编辑其中一个时按下了其中一个箭头键。功能
override func keyDown(theEvent: NSEvent) {
switch theEvent.character {
case NSRightArrowFunctionKey:
println(1)
moveGor(NSRightArrowFunctionKey)
case NSLeftArrowFunctionKey:
moveGor(NSLeftArrowFunctionKey)
case NSUpArrowFunctionKey:
moveVert(NSUpArrowFunctionKey)
case NSDownArrowFunctionKey:
moveVert(NSDownArrowFunctionKey)
default:
super.keyDown(theEvent)
}
}
Run Code Online (Sandbox Code Playgroud)
似乎不起作用。有没有其他方法可以快速做到这一点?
编辑:
我有以下扩展名NSEvent
:
extension NSEvent {
var character: Int {
let str = charactersIgnoringModifiers!.utf16
return Int(str[str.startIndex])
}
}
Run Code Online (Sandbox Code Playgroud)
我在之前的函数中使用的