OSX Cocoa: How to check which window is in focus?
cocoa, macos
Solution
There are actually two forms of “focus”:
- The window that has the active appearance (colored/dark gray stoplight, black text in title bar, undimmed proxy icon, etc.) is the main window.
- The window that receives key events is the key window.
Consider a document window with an Inspector panel. If the user is editing some text in a field in the Inspector, then the document window may be main, but the Inspector panel is key.
To get the key window or the main window, ask the shared NSApplication instance. And, as TechZen noted, you can ask a window both whether it is key and whether it is main.
Problem
Hey all, I'm pretty new to Cocoa and XCode, so I'm sorry if this is a dumb question. How would I go about checking which window is in focus? Additionally how would I go about checking if a certain window is in focus, if this is not the same as the previous solution. Thanks for the help.