I have a windowController with a NSTableView and a textfield which I use to search data in my tableView
In the controller, I intercept the keydown event and if it is a character, I want to fill the textfield with this character and give the focus to the textfield
So I write
@objc override func myKeyDown(with event: NSEvent) -> Bool {
if ctrlRch != nil {
window?.makeFirstResponder(ctrlRch)
Swift.print("firstResponder is \(window?.firstResponder)")
ctrlRch.stringValue = event.characters!
var fieldEditor = ctrlRch.currentEditor()
fieldEditor?.selectedRange = NSMakeRange(ctrlRch!.stringValue.count-1, 1)
return true
}
But the caret (or the cursor) is not positionned in the textfield
What I missed?
All is my fault
The lesson of it is we have to think much more harder before making a post
My subclassed control, in the override function becomeFirstResponder didn't called super.becomeFirstResponder (the function of the NSTextField). As a result, currentEditor() of the control returned nil.
When calling super.becomeFirstResponder, the function currentEditor returns the desired editor.
Sorry about all this