开发者

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

开发者 https://www.devze.com 2022-12-15 17:41 出处:网络
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?

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 soluti开发者_StackOverflow中文版on.

Thanks for the help.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消