I show my main window by calling
[window makeKeyAndOrderFront:self];
[NSApp activateIgnoringOtherApps:YES];
[window setIsVisible:YES];
[window display];
which works, but doesn’t set the window to the key window right after this calls. I have to wait “some time” until [NSApp keyWindow] returns the actual window.
I’m wondering now, how long does this take and how can I force a window to become the key window immediately?
I think there are probably good reasons that
makeKeyAndOrderFrontisn’t a synchronous call, namely there could be coordination involved with multiple windows and objects thatNSApplicationneed to take care of to make it happen, so forcing window to become key immediately is probably not supported by Cocoa. This however may not be a problem depending on the problem you are trying to solve.Now, my guess is that some of your methods depend on the window being key, and at the moment they are not happening properly because the window doesn’t become
keyimmediately. However, you can implement theNSWindowDelegateprotocol, set yourself as window delegate, and override- (void)windowDidBecomeKey:(NSNotification *)notificationmethod to find out when the window did become key. This should also be a global notification in case that works better for you.For more details, check out apple docs at http://developer.apple.com/library/mac/#documentation/cocoa/reference/NSWindowDelegate_Protocol/Reference/Reference.html