tags:

views:

100

answers:

2

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.

+1  A: 

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.

TechZen
+4  A: 

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.

Peter Hosey
Thanks for the help, I understand the idea of what I should be doing but I'm having trouble putting it into practice.How exactly do I go about asking the shared NSApplication instance?I've been working through some tutorials but I'm still fairly new to this type of programming. So I'm sorry if I'm asking what seems to be a stupid question.If you could provide me with a code sample that would be a great help.Thanks again.
jbx
Follow the links I provided.
Peter Hosey