Hiding an element by clicking somewhere off of it

Kind of a weird way of asking it so let me explain. I have a custom view that acts as a menu. You click a push button and the menu appears. But what I need is for it to hide if you click somewhere else outside of the custom view so it will react like an actual menu would.

Is there a way of doing this other than recording every click and determining which element I clicked, then if it’s not the custom view, hide it?

Thanks for the help.

I don’t think you need to record every click, but I don’t see any other way to do this other than noting any clicks with both a global monitor and a local monitor until you see the first one outside your view, then you can stop monitoring until the button is clicked again. Unfortunately, addGlobalMonitorForEventsMatchingMask:handler: and addLocalMonitorForEventsMatchingMask:handler: (both of NSEvent) use blocks, so they can only be used in ObjectiveC.
See this link, http://macscripter.net/viewtopic.php?id=34657, for some example code.


Ouch! Not what I was hoping to hear.
I just realized something about the default behavior of menus. When you click, on the menu or desktop or otherwise, the menu closes. Maybe I can work with that rather than worrying about whether its in my view or not.

Thanks for the input and sorry for the delay.

You can subclass menus and popups. I have some example files around where images are added into menus. So it might be more hard core Cocoa but it’s possible.