Cocoa - How to get Mouse Down Events when mouse isn't Dragging?
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 moremouseD开发者_JS百科own
events are generated.
Question: how do I determine that the mouse is still down after that initial (and only) mouseDown
event?
Thanks.
If you need to do something periodically while the mouse is down, have mouseDown:
create a repeating timer, and have mouseUp:
tear it down.
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.
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft));
is what you are after on 64 bit. (ok 4 years late).
// mozzilla...
1.30 #ifdef XP_MACOSX
1.31 - // hacky OS call to ensure that we don't show a context menu when the user
1.32 - // let go of the mouse already, after a long, cpu-hogging operation prevented
1.33 + // Hack to ensure that we don't show a context menu when the user
1.34 + // let go of the mouse after a long cpu-hogging operation prevented
1.35 // us from handling any OS events. See bug 117589.
1.36 - if (!::StillDown())
1.37 + if (!CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft))
1.38 return;
1.39 #endif
精彩评论