How to create “No Activate” window in Mac OS
-
09-06-2021 - |
Question
In Windows platform a window has an attribute named SWP_NOACTIVATE that causes the window displayed inactive and also after mouse click, that window does not gain focus, It is useful for example to virtual keyboards that you can type by mouse click. what is equivalent solution in Mac? In other words how can I implement a window that does not change focus on click it? thanks.
Solution
You'll want to read Key and Main Windows in the Window Programming Guide. Briefly, create your own NSWindow subclass and override -canBecomeKeyWindow
and -canBecomeMainWindow
so that both return NO:
...a window that acts as a floating palette of items that are only dragged out by mouse actions never needs to be the key window. Such a window can be defined as a subclass of NSWindow that overrides the methods canBecomeKeyWindow and canBecomeMainWindow to return NO instead of the default of YES. Defining a window this way prevents it from ever becoming the key or main window.
OTHER TIPS
You put "Cocoa" and "Carbon" tags on this question but since nobody should be doing Carbon anymore, I'll try to address your question just with Cocoa.
Check out these two NSWindow
methods: canBecomeKeyWindow
and canBecomeMainWindow
. Consider subclassing NSWindow and returning "NO
" for either (or both) of these things when you want your window to be un-focusable.
There's also an intriguing method called setIgnoresMouseEvents:
, which makes the window "transparent" to mouse clicks and events.