Set focus on NSTextField in an NSMenuItem
Set focus on NSTextField in an NSMenuItem
- Subject: Set focus on NSTextField in an NSMenuItem
- From: Dragan Milić via Cocoa-dev <email@hidden>
- Date: Tue, 18 Oct 2022 18:43:38 +0200
Hi all,
I’m developing a tiny application, which has an NSStatusItem instance with a
menu. One for the menu items of the menu has a custom view, which is either an
NSTextField instance, or in can even be an NSView instance containing the
NSTextField instance (the latter approach enables me to add some borders around
the text field). However, I have problems setting focus on that text field.
Clicking the text field does nothing and if I click on it repeatedly a couple
of times, the application hangs (and eventually crashes), getting into the
infinite loop of -[NSMenuWindowManagerWindow setFirstResponder:] messages, even
stealing keyboard events from all other applications while hanging!
In essence, I can’t set focus on the text field nor type anything into it. I
had this problem on older versions of macOS (Catalina and older), but after
clicking on the text field a couple of times, it would get the focus
eventually. On Big Sur and later, it’s not possible, and clicking on the text
field repeatedly hangs and crashes the application.
This issue isn’t related to the menu being attached to the NSStatusItem, it
also happens in the main and/or contextual menu within the application. I’d
eventually want to set the focus on the text field programmatically each time
the menu appears (so a user can start typing immediately, something like
Spotlight used to do in the older versions of macOS), but that’s not possible
either.
The only similar attempt I could find is dated back in 2008 on this very list,
you can find it here:
https://lists.apple.com/archives/cocoa-dev/2008/Jan/msg00138.html
But even at that time, the problem was hard to solve.
I know NSMenu has its own track of the event loop and I assume that’s what
causes the problem. Action-On-Click oriented controls (NSButton,
NSPopUpButton…) work fine embedded in an NSMenuItem instance, but not
NSTextField. I wonder if what I want to achieve is even possible. If so, I’d
really appreciate if someone can offer the solution or point me in the right
direction for further investigation and solution attempts.
Cheers,
-- Dragan
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden