本文介绍了使用NSTextView和Swift对Command-Enter执行操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

NSTextField 可以连接到 IBAction ,并在用户按下回车键时调用它。 p>

当用户按Command-Enter(或Option-Enter等)时,如何使用 NSTextView



阅读文档导致我认为它与 doCommandBySelector:有关,但我找不到任何示例。

解决方案

您可以覆盖keyDown事件并检查返回键代码== 36(返回键) = 76(输入密钥)和modifierFlags = CommandKeyMask。尝试如下:

  override func keyDown(theEvent:NSEvent){
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)
}
}
pre>

An NSTextField can connect to an IBAction 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执行操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-07 01:10