Kingdom Kao
Kingdom Kao

Reputation: 11

How could I detect mouseDown event outside my NSView?

I create my custom NSView when a certain button is clicked, and I want to close this custom view if your click outside the NSView. But it seem that I could not receive mouseDown event in this custom NSView. Is there anyone know how to fit it ? Many thanks.

Upvotes: 1

Views: 1906

Answers (2)

uchuugaka
uchuugaka

Reputation: 12782

I wanted to share two other solutions that unlike the NSEvent approach, doesn't have the sandbox limitations.

A cover window. It's more involved code and a good exercise in learning what's possible with windows and views. But worth learning and knowing.

Create a borderless window that takes up the whole screen. The borderless window can then handle mouse events.

A tracking area. This approach is similar

Upvotes: 1

uchuugaka
uchuugaka

Reputation: 12782

you want to use NSEvent global event monitor. Then you may need to convert the mouse NSPoint to your view coordinates and use the function NSPointInRect to see if it is inside or not.

Upvotes: 0

Related Questions