Rapidly clicking left and right mouse buttons causes mouseUp events to be lost

I have a simple card game that relies on mouseDown, mouseDragged and mouseUp functions. With a single-button mouse everything works great. However, if I connect my Logitech Marathon Mouse and rapidly click around creating mouse down, drag and up events with both buttons, at some point the cards stop in completely incorrect places. The code will position the cards correctly automatically, if the card is no longer played. However this status is set in mouseUp, which in this error condition never gets triggered. I've tried overriding the rightMouseDown etc. functions, but it doesn't seem to make a difference. With fast simultaneous mouse events it seems they can get lost. Anything I can do about this?

Replies

Events should not be getting lost.


The first thing you should do is analyze the problem a little more carefully. You need to be certain that events are truly never arriving (and not assume that because the code to calculate locations doesn't work, since that could be a different bug). For example, you can keep a counter, which you increment in mouseDown and decrement in mouseUp, and after a while check that the counter is zero after you pound on the mouse for a while.


Do you have any code that gets the current mouse location, outside of the event queue (that is, gets mouse locations other than from the NSEvent "locationInWindow" property)? If so, you have to be very careful, but the locations in the NSEvents are correct at the time the event was queued, and may not represent match the current "real" mouse position. Mixing the two ways of getting locations is going to break under the kind of scenario you described.

Thanks for your thoughts QuinceyMorris.


I created a super-simple new project to demonstrate this. https://bitbucket.org/tommiur/mousebug/overview


All modified code is in GameScene class. If I run this on my macOS Sierra and do simultaneous left and right clicks the counters start building up (which they should not if mouseUp always matches mouseDown). If I try the same with the Magic Mouse 2, there is no problem. Not sure if this is a problem in the Logitech drivers, macOS or SpriteKit, but I would suspect SpriteKit, since otherwise the mouse works fine.


Edit: It seems I found a workaround by implementing a counter to the Sprite. If mouseDown is pressed (any button) AND counter is zero, that counter is increased to 1 and mouseDown is run. At mouseUp the counter is reduced to zero. It seems that the relation at the moment is that there can be multiple downs per one up, not sure if this intentional but the code can be adjusted to it.