How can I know when an NSTextField gets input focus (first responder)?

I think I can do this by creating a custom view overriding the following 2 methods:

@implementation MyTextField

- (BOOL)becomeFirstResponder {
    NSLog(@"%s: %@", __func__, self);
    return [super becomeFirstResponder];
    //return YES;
}

- (BOOL)resignFirstResponder {
    NSLog(@"%s: %@", __func__, self);
    return [super resignFirstResponder];
    //return YES;
}

@end

But there is a gotcha - I always get successive become/resign. That is when I get becomeFirstResponder, I also get resignFirstResponder immediately. Note - I only have one MyTextField instance on my window.

Is there another safe way to achieve my goal?

I found out why myself.

I create a custom NSWindowController:

@implementation MainWC

- (void)windowDidLoad {
    [super windowDidLoad];
    
    MainVC* vc = (MainVC*)self.contentViewController;
    [vc windowLoaded];
}

@end

And in MainVC:

- (void)windowLoaded {
    [self.view.window addObserver:self
                       forKeyPath:@"firstResponder"
                          options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld
                          context:nil];
}

- (void)observeValueForKeyPath:(NSString *)keyPath
                      ofObject:(id)object
                        change:(NSDictionary<NSKeyValueChangeKey,id> *)change
                       context:(void *)context
{
    NSView* oldView = change[NSKeyValueChangeOldKey];
    NSView* newView = change[NSKeyValueChangeNewKey];
    NSLog(@"%@ (%@) ====> %@ (%@)", oldView.identifier, oldView.class, newView.identifier, newView.class);
}

It seems when a textfield is focused, it immediately resigns and gives focus to its inner textview:

2023-03-24 18:02:54.841247+0800 ObjcTest[25855:1521206] _NS:10 (NSWindow) ====> normal (NSTextField)
2023-03-24 18:02:54.846902+0800 ObjcTest[25855:1521206] normal (NSTextField) ====> (null) (NSTextView)
2023-03-24 18:02:56.032290+0800 ObjcTest[25855:1521662] Sleep done in q
2023-03-24 18:03:09.653850+0800 ObjcTest[25855:1521206] (null) (NSTextView) ====> _NS:10 (NSWindow)
2023-03-24 18:03:09.657517+0800 ObjcTest[25855:1521206] _NS:10 (NSWindow) ====> mytextfield (MyTextField)
2023-03-24 18:03:09.657677+0800 ObjcTest[25855:1521206] -[MyTextField becomeFirstResponder]: <MyTextField: 0x7f8dfa123eb0>
2023-03-24 18:03:09.659349+0800 ObjcTest[25855:1521206] -[MyTextField resignFirstResponder]: <MyTextField: 0x7f8dfa123eb0>
2023-03-24 18:03:09.659522+0800 ObjcTest[25855:1521206] mytextfield (MyTextField) ====> (null) (NSTextView)
How can I know when an NSTextField gets input focus (first responder)?
 
 
Q