11

I am making a application that the user will have to interact with one window and when they have that window configured the way they want it they switch to a different application then my application will begin to do other stuff which i will have defined in a method

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

Thanks

4

2 回答 2

14

That is not the same as key window. Key window means that you will receive input events for your application. What you want to know is whether your application is in the foreground. What you're looking for is the NSApplication notifications NSApplicationDidBecomeActiveNotification and NSApplicationDidResignActiveNotification. Observe those to discover when your application is or isn't in the foreground. Your application delegate's applicationDidBecomeActive: and applicationDidResignActive: will automatically be called on these events.

于 2010-06-24T05:05:32.727 回答
12

[NSWindow isKeyWindow] might be what you're looking for.

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

- (BOOL)isKeyWindow

Return Value 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 stops being key, check out the NSWindowDidBecomeKeyNotification and NSWindowDidResignKeyNotification notifications.

于 2010-06-24T05:02:10.143 回答