Detecting if a window is a key window in cocoa

I am making an application that the user will have to interact with one window, and when they have this window configured as they want, they switch to another application, then my application will start to do other things that I will define in the method

say for an example program, when the main window has focus, it contains a label that says: "I am focused", and when a person clicks on the desktop or another window / application, then the label will read "I'm not focused."

thanks

+7
cocoa nswindow
source share
2 answers

This is not the same as the key window. A key window means that you will receive input events for your application. You want to know if your application is in the foreground. You are looking for NSApplication NSApplicationDidBecomeActiveNotification and NSApplicationDidResignActiveNotification . Watch to find out when your application is or is not in the foreground. Your application delegate applicationDidBecomeActive: and applicationDidResignActive: will be automatically called in these events.

+9
source share

[NSWindow isKeyWindow] may be what you are looking for.

Indicates whether the window is the key window for the application.

- (BOOL) isKeyWindow

The return value is YES if the window is the key window for the application; otherwise NO.

If you want to detect when your window becomes key or when it ceases to be key, check the NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification notifications.

+11
source share

All Articles