Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I cede focus to the previous application in Cocoa?

Let's saying the user is currently on Chrome. So I have a HUD that is invoked by a global hotkey, the user then types something into it and on clicking Close or OK, the HUD goes away (just using [NSPanel orderOut:]) but my application is still focused (i.e. the Menu bar of my application is still visible). But I want the focus to go back to original application, Chrome. How do I achieve something like this? Ideally, I'd also be able to never "get" focus to my application and hence, the HUD input would all happen while Chrome was still the focused application. Things.app manages to achieve something like this. Quicksilver does this too.

like image 637
abiraja Avatar asked Feb 03 '23 07:02

abiraja


2 Answers

Call [NSApp hide:]

That will hide your application and activate the previously-active application.

like image 111
Darren Avatar answered Feb 06 '23 08:02

Darren


In order to never get the focus on your application call [panel setStyleMask:[panel styleMask] | NSNonactivatingPanelMask]; on your NSPanel or check the Non Activating checkbox on your NSPanel in Interface Builder.

like image 31
0xced Avatar answered Feb 06 '23 08:02

0xced