Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSX Cocoa: How to check which window is in focus?

Tags:

macos

cocoa

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.

like image 790
jbx Avatar asked Jan 05 '10 15:01

jbx


2 Answers

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.

like image 101
Peter Hosey Avatar answered Oct 18 '22 12:10

Peter Hosey


You looking for 'keyWindow'. It's a property of both NSWindow and NSApplication. It's a bool in the former and a NSWindow instances in the latter.

like image 34
TechZen Avatar answered Oct 18 '22 12:10

TechZen