4
votes

I'm having a problem finding out which NSTextfield is focused.

I am building a multi-language form and have several NSTextfields for data entry. I have to change the text input source for some of the NSTextfields during data entry, and I need it to happen automatically.

For now, I can change the text input source as I mentioned here without problem.

The problem that I have is to change the input source right when the NSTextfield becomes focused. If I use the controlTextDidBeginEditing: delegate method it changes the source input after typing the first letter. This means that I lose the first word I typed in proper language.

Is there any delegate to find it ?

2

2 Answers

0
votes

You can subclass your NSTextField and override - (BOOL)becomeFirstResponder (NSResponder) to respond to this kind of event.

You can also try control:textShouldBeginEditing: instead.

0
votes

You will need to subclass NSTextField

Swift 3+

class FocusingTextField : NSTextField {

    var isFocused : Bool = false

    override func becomeFirstResponder() -> Bool {
        let orig = super.becomeFirstResponder()
        if(orig) { self.isFocused = true }
        return orig
    }

    override func textDidEndEditing(_ notification: Notification) {
        super.textDidEndEditing(notification)
        self.isFocused = false
    }

    override func selectText(_ sender: Any?) {
        super.selectText(sender)
        self.isFocused = true
    }

}

self.view.window?.firstResponder inside your view controller would give a NSTextView