问题描述
NSTextField
可以连接到 IBAction
,并在用户按下回车键时调用它。 p>
当用户按Command-Enter(或Option-Enter等)时,如何使用 NSTextView
?
阅读文档导致我认为它与 doCommandBySelector:
有关,但我找不到任何示例。
您可以覆盖keyDown事件并检查返回键代码== 36(返回键) = 76(输入密钥)和modifierFlags = CommandKeyMask。尝试如下:
override func keyDown(theEvent:NSEvent){
pre>
if theEvent.keyCode == 36& & theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask)== .CommandKeyMask {
print(command-return pressed)
}
如果Event.keyCode == 76& theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask)== .CommandKeyMask {
print(command-enter pressed)
}
}
An
NSTextField
can connect to anIBAction
and will call it when the user hits the enter key.How can I perform an action with an
NSTextView
when the user presses Command-Enter (Or Option-Enter, etc)?Reading the documentation leads me to think it's something to do with
doCommandBySelector:
but I can't find any examples.解决方案You can override keyDown event and check if return key code == 36 (return key) or and key code == 76 (enter key) and modifierFlags = CommandKeyMask. Try as follow:
override func keyDown(theEvent:NSEvent) { if theEvent.keyCode == 36 && theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask) == .CommandKeyMask { print("command-return pressed") } if theEvent.keyCode == 76 && theEvent.modifierFlags.intersect(.DeviceIndependentModifierFlagsMask) == .CommandKeyMask { print("command-enter pressed") } }
这篇关于使用NSTextView和Swift对Command-Enter执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!