如何从 NSTextField 的字段编辑器中拦截击键? [英] How to intercept keystrokes from within the field editor of an NSTextField?
问题描述
当我的自定义 NSTextField
处于文本编辑模式"并且 field editor
已作为 firstResponder
放在它前面时,我没有不再通过 NSTextField.keyDown(...)
获取击键.我知道现在正在通过 field editor
路由击键.大多数在线建议是在自定义 NSTextField
的委托中覆盖以下方法:
When my custom NSTextField
is in "text editing mode" and the field editor
has been placed in front of it as firstResponder
I no longer get the keystrokes through NSTextField.keyDown(...)
. I understand that the keystrokes are now being routed through the field editor
. Most online recommendations are to override the following method within the delegate of the custom NSTextField
:
control(_:textView:doCommandBySelector:)
我确实覆盖了这个方法,但它似乎没有被调用?见下面的代码:
I have indeed overridden this method but it doesn't seem to get called? See code below:
class FocusDelegate: NSObject, NSTextFieldDelegate{
func control(control: NSControl, textView: NSTextView, doCommandBySelector commandSelector: Selector) -> Bool {
println("FocusDelegate")
if commandSelector == Selector("cancelOperation:"){
println("cancelOperation")
control.abortEditing()
return true
}
return false
}
}
我唯一要做的另一件事是将我的自定义 NSTextField
的委托设置为上述类的一个实例.我还尝试将上面的方法直接放入自定义 NSTextField
类 - 它不会导致任何错误,但也不会被调用.
The only other thing I do is set the delegate of my custom NSTextField
to an instance of the class above. I have also tried placing the method above directly into the custom NSTextField
class - it doesn't cause any errors but it also doesn't get called.
- 在让委托呼叫工作时,我在上面遗漏了什么?
- 除了创建我自己的自定义字段编辑器之外,这是唯一的方法吗?可以不询问
textDidChange(notification: NSNotification)
以生成按下的键吗? - 在使用
control(_:textView:doCommandBySelector:)
委托方法时,我如何捕获不具有标准键绑定的键的按下.特别是,我想拦截未映射到任何标准选择器的组合键shift+enter".(暗示问题:你能不能只映射到 NSResponder 中的标准键操作方法?)
- What am I missing above in getting the delegate call to work?
- Other than creating my own custom field editor is this the only way? Can the
textDidChange(notification: NSNotification)
not be interrogated to yield the keys pressed? - In using the
control(_:textView:doCommandBySelector:)
delegate method, how do I trap the pressing of keys that do NOT have standard key bindings. In particular, I want to intercept the key combination "shift+enter" which does not map to any standard selector. (Implied Question: can you only map to standard key-action methods in NSResponder?)
推荐答案
我相信我对 NSTextField
的委托及其 fieldEditor 及其委托的工作方式更加清晰.是的,每个都有自己的委托,NSTextField
会自动设置为 fieldEditor
的委托.
I believe I have achieved greater clarity with regard to the workings of NSTextField
its delegates and its fieldEditor and its delegates. Yes, each one has its own delegate and the NSTextField
is automatically set up as the delegate of the fieldEditor
.
我正在向主机 NSTextField
添加一个委托.当 fieldEditor
是 firstResponder
时,这将 NOT 被调用,这正是我所关心的.这也不是上述场景的重要代表.
I was adding a delegate to the host NSTextField
. This will NOT get called when the fieldEditor
is firstResponder
, which was what I was concerned with. This is also not the important delegate for the scenario above.
似乎有用的是使 NSTextField
符合 NSTextViewDelegate
协议并特别覆盖该方法:
What appears to be useful is making the NSTextField
conform to the NSTextViewDelegate
protocol and specifically overriding the method:
func textView(textView: NSTextView, shouldChangeTextInRange affectedCharRange: NSRange, replacementString: String) -> Bool
捕获各种按键.
textDidChange(notification: NSNotification)
中的通知在这种情况下没有用,因为它被报告AFTER 按键.
The notification in textDidChange(notification: NSNotification)
is not of use in this scenario because it is reported AFTER the keypress.
这篇关于如何从 NSTextField 的字段编辑器中拦截击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!