How to intercept keystrokes from within the field editor of an NSTextField?

大憨熊 提交于 2019-12-22 08:25:18

问题


Intro

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
    }
}

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.

Questions

  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?)

回答1:


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.

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.

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

to trap various keypresses.

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



来源:https://stackoverflow.com/questions/28901733/how-to-intercept-keystrokes-from-within-the-field-editor-of-an-nstextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!