James Eichele
James Eichele

Reputation: 119144

In Cocoa, how can my NSView receive an event when the mouse is held down (but not moved)?

I'm looking for the right way to handle "mouse held down in one spot" events in my NSView subclass.

I am familiar with Cocoa's mouseDragged: event, but it is only triggered when the mouse moves. If the mouse stays in the same position, no drag event is triggered. Similarly, mouseDown: is only fired when the button is first pressed. My view needs to perform an action as long as the mouse is held down in a particular region.

What is the proper way to do this kind of thing?

Upvotes: 1

Views: 1112

Answers (2)

Dan Messing
Dan Messing

Reputation: 828

I'm not sure exactly what you're trying to accomplish, but if you want an action to be repeated at set time intervals after the mouseDown:, you could set a recurring NSTimer in the mouseDown: method that gets cancelled as soon as there is a mouseDragged: or mouseUp: event.

Upvotes: 2

mipadi
mipadi

Reputation: 410662

Can you start performing the action when you receive a mouseDown: event, and stop when you receive mouseUp: (or mouseDragged:, if you want to stop then, too)?

Upvotes: 3

Related Questions