Cocoa - 当鼠标不拖动时如何获取鼠标按下事件?
可可,Mac OS X 10.6。
我的应用程序(游戏)需要重复确定鼠标是否在视图内按下,即使鼠标位置没有改变。
问题:
mouseDown
只会在第一次按下鼠标时被调用。如果此时鼠标未移动,则不会调用mouseDragged
,并且不会生成更多mouseDown
事件。
问题:如何确定在初始(也是唯一)mouseDown
事件之后鼠标仍然处于按下状态?
谢谢。
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 moremouseDown
events are generated.
Question: how do I determine that the mouse is still down after that initial (and only) mouseDown
event?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您需要在鼠标按下时定期执行某些操作,请使用
mouseDown:
创建一个重复的 计时器,并让mouseUp:
将其拆除。If you need to do something periodically while the mouse is down, have
mouseDown:
create a repeating timer, and havemouseUp:
tear it down.如果您只想知道鼠标是否按下,请在
mouseDown:
和mouseUp:
中设置一个BOOL
变量。如果您想在鼠标按下时继续执行某些操作,可以输入鼠标跟踪循环,如下所述:鼠标跟踪循环方法 基本上,您会循环直到找到鼠标松开事件,并且你可以在循环中做任何你想做的事情。您必须使用
nextEventMatchingMask:untilDate:inMode:dequeue:
而不是nextEventMatchingMask:
并将untilDate
设置为较短的值。If you just want to know whether the mouse is down or not, set a
BOOL
variable inmouseDown:
andmouseUp:
.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 ofnextEventMatchingMask:
and set theuntilDate
to something short.CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft));
这就是您在 64 位上所追求的。 (好吧晚了4年)。
// 莫兹拉...
CGEventSourceButtonState(kCGEventSourceStateCombinedSessionState, kCGMouseButtonLeft));
is what you are after on 64 bit. (ok 4 years late).
// mozzilla...