Search code examples
swiftmacosaccessibilitynswindownspanel

How can I make an OS X application unfocusable while still receiving click events?


I'm developing an on screen keyboard application for OS X, similar to the one that's built in to the operating system (Keyboard Viewer). I seem to have hit a wall as I'm not sure how I can accept click events from buttons and not steal focus from the currently activated application. I know this is possible since there are apps that already do this, e.g. AssistiveWere's KeyStrokes.

So my question is this: How can I make my window receive mouse events and handle them without getting activated?

P.S. I'm not very experienced in OS X development and this is my first Swift project, so excuses if this is a trivial problem.


Solution

  • You need to make your window an instance of NSPanel (or a subclass), include NSNonactivatingPanelMask in its styleMask, and set becomesKeyOnlyIfNeeded to true. (The style mask can be controlled in IB.) You probably also want it to be floating so it's always above normal windows, so set floatingPanel to true, too.