views:

39

answers:

2

Hi all,

Cocoa, Mac OS X 10.6.

My app (a game) needs to determine if the mouse is down within a view, repeatedly, even if the mouse position doesn't change.

The problem:

  • mouseDown will only be called the first time the mouse is pressed and held down. If the mouse is not moved at this point, mouseDragged is not called, and no more mouseDown events are generated.

Question: how do I determine that the mouse is still down after that initial (and only) mouseDown event?

Thanks.

A: 

If you need to do something periodically while the mouse is down, have mouseDown: create a repeating timer, and have mouseUp: tear it down.

Peter Hosey
Thanks Peter. It seems a bit unwieldy -- would have preferred a system level approach -- but will try this. It seems a curious omission on the Cocoa team's part to me, since it sorta pushes people to a polling approach which is not great. Cheers.
SirRatty
SirRatty: Timers are not polling.
Peter Hosey
A: 

If you just want to know whether the mouse is down or not, set a BOOL variable in mouseDown: and mouseUp:.

If you want to keep doing something while the mouse is down, you can enter a mouse tracking loop as explained here: The Mouse-Tracking Loop Approach Basically you loop until you find the mouse up event, and you can do whatever you want in the loop. You will have to use nextEventMatchingMask:untilDate:inMode:dequeue: instead of nextEventMatchingMask: and set the untilDate to something short.

Tom Dalling
Thanks Tom. Since it's a game, I need to interpret these games asynchronously. That is, over time, so I need to get the event then pass them off to the game logic. (Thus I can't stay in a tight loop.)
SirRatty

related questions