如何从NSTextField的字段编辑器中拦截击键? [英] How to intercept keystrokes from within the field editor of an NSTextField?

查看:133
本文介绍了如何从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.

  1. 在让代表打电话去上班时,我在上面遗漏了什么?
  2. 除了创建自己的自定义字段编辑器之外,这是唯一的方法吗?可以不问textDidChange(notification: NSNotification)来产生按下的键吗?
  3. 在使用control(_:textView:doCommandBySelector:)委托方法时,如何捕获没有具有标准键绑定的键的按下.特别是,我想截取没有映射到任何标准选择器的组合键"shift + enter". (暗示的问题:您只能映射到NSResponder中的标准按键操作方法吗?)
  1. What am I missing above in getting the delegate call to work?
  2. 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?
  3. 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.当fieldEditorfirstResponder时,将 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)中的通知在这种情况下不可用,因为它是在按键后 报告的.

The notification in textDidChange(notification: NSNotification) is not of use in this scenario because it is reported AFTER the keypress.

这篇关于如何从NSTextField的字段编辑器中拦截击键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆