4

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 ?

Community
  • 1
  • 1
Prooshani
  • 534
  • 7
  • 20

2 Answers2

0

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

twodayslate
  • 2,803
  • 3
  • 27
  • 43
0

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

You can also try control:textShouldBeginEditing: instead.

AliSoftware
  • 32,623
  • 6
  • 82
  • 77
  • @AliSoftware, Nope, sorry my friend but `control:textShouldBeginEditing` works similar to the `controlTextDidBeginEditing:` method ! and both didn't work for me ! I need to change the **input source** before typing anything ( right after selecting the textfield) and both methods changes **input source** after start typing . it means I will lose first letter ( As I am ). – Prooshani Jun 25 '11 at 15:24
  • @AliSoftware, And Also becomeFirstResponder is available in Mac OSx 10.0 through Mac OSx 10.4 and I am using Mac OSx 10.6. – Prooshani Jun 25 '11 at 15:38
  • `becomeFirstResponder` is available since 10.0 and was never removed from the API, so it is still available in 10.6 (hopefully!!). See the [documentation](http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSResponder_Class/Reference/Reference.html). I just tried it myself on a 10.6-only project to confirm this and it works like a charm. – AliSoftware Jun 25 '11 at 16:20
  • @AliSoftware, Hopefully this is true and becomefirstResponder did not removed from API and this [documents](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/DeprecationAppendix/AppendixADeprecatedAPI.html#//apple_ref/occ/instm/NSTextView/becomeFirstResponder) are all nothing. Would you please give me a hint or small code to show me how you could use this method to find which control ( NSTextfield here ) is became first responder ? – Prooshani Jun 25 '11 at 16:27
  • 1
    The document in your answer is about NSTextView and not NSTextField. NSTextView had its own `becomeFirstResponder` implementation (as you can see in the documentation, this method was directly defined in NSTextField's documentation and was a different one from the NSResponder's `becomeFirstResponder` method) and that is the one which is deprecated. The one from NSResponder (for which I gave the link to the doc earlier) is not deprecated and still available in 10.6 – AliSoftware Jun 25 '11 at 16:46
  • 1
    @interface MyTextField : NSTextField @end @implementation MyTextField -(BOOL)becomeFirstResponder { NSLog(@"The textField %@ did get the focus.",self); // you code for changing the dataSource here return [super becomeFirstResponder]; } Then change the class of your NSTextField objects in your XIB files to MyTextField – AliSoftware Jun 25 '11 at 16:48
  • @AliSoftware,You know, I have too many NSTextField already defined and working now , they have controltextdidchanged delegate and many other functions and delegates that used to work with. It's seems that this is impossible for me to change all of them and subclass them to new subclass. Is there any other way to do this ? without sublassing ? – Prooshani Jun 26 '11 at 18:25