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