Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Cocoa, what is the best way to change the cursor when hovering over a circular view?

What I want to do

I have a circular custom NSView subclass, and I want to change the appearance of the cursor when the mouse is hovering over the circular portions of the view, but not when over the portions of the view's rectangle that fall outside the circle.

Here's an illustration. I would have inlined it with an image tag, but I'm too new to partake of such awesome features.

What I know so far

I know how to change the appearance of the cursor through NSCursor. I think that the best way to accomplish this for a rectangular view would be with a cursor rectangle. I know that I could receive mouseMoved events (and ought to turn them off when the mouse isn't over this view, using mouseEntered and mouseExited), and have a simple, inexpensive way to determine if a point lies in the region of interest.

So what's the problem then?

As far as I can tell, the system does not send mouseMoved events to a view that is not the first responder. Therefore, if I want to get mouseMoved events when the mouse is hovering over my view, I need to steal firstResponder status from whoever currently has it. If a text view has the focus, simply moving the mouse over such a view would steal it away, which is simply unacceptable from a usability standpoint.

Therefore, my question boils down to: is there a better way to do this? Can I get mouseMoved events without being the first responder?

Thanks!

I would have added the following tags: custom-views mouse-events NSCursor firstResponder But again, I'm an SA noob so I can't.

like image 923
Ryan Ballantyne Avatar asked Nov 21 '25 01:11

Ryan Ballantyne


2 Answers

I think you may just need to override the acceptsFirstResponder method in your NSView to return YES. If you don't, then the view won't receive any event information.

like image 70
Dave DeLong Avatar answered Nov 23 '25 13:11

Dave DeLong


Does it have to be a circular area? If anywhere within the rectangular bounds of the view is acceptable, you could use a cursor rect.

like image 36
Peter Hosey Avatar answered Nov 23 '25 15:11

Peter Hosey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!