Possible bug of getLengthOfMousePress()


#1

I’m experiencing some problems with the getLengthOfMousePress() call of MouseEvent.

I have my mouseDrag method continuously called because of a beginDragAutoRepeat called in the mouseDown.

On the first call to mouseDrag, the return value of getLengthOfMousePress() is zero

On subsequent calls to mouseDrag (within a secondo to the first one), the return value of getLengthOfMousePress() is really huge, in the order of millions or billions, which is obviously too high to be the correct number of milliseconds elapsed.

On PC, all works well, so maybe there is some problem in the code that calculates that time on the mac?


#2

I just tried it and it seems to be working correctly… the mac gives the time in seconds, and I multiply by 1000 - there’s not really much to go wrong there.


#3

I’ve done some investigations:

the timestamp of the most recent mouse down event is contained in the variable
juce_recentMouseDownTimes[0]
which is initialised this way in ComponentPeer::handleMouseDown:

juce_recentMouseDownTimes[0] = time 

where time is an argument to that method, which in turn is called by

NSViewComponentPeer::redirectMouseDown

with the following arguments:
handleMouseDown (x, y, getMouseTime (ev));

static int64 getMouseTime (UIEvent* e)  { return (int64) [e timestamp] * 1000.0; }

The documentation says that timestamp is a property that contains the number of seconds since system startup.

On the other hand, the subsequent mouseDrag events I receive are built from the internalMouseDrag method, which is often called from timerCallback of InternalDragRepeater (I’m using the beingDragAutoRepeat feature) this way:

c->internalMouseDrag (x, y, Time::currentTimeMillis());

Thus, the drag event is being built with a timestamp that is the number of milliseconds since the Epoch, not since system startup.

This causes the difference returned by getLengthOfMousePress so high.


#4

Ah! Sorry, I didn’t notice the bit where you said you were using the drag repeater. Ok, that’s a good catch and should be a simple fix - how about this:

static int64 getMouseTime (NSEvent* e) { return (Time::currentTimeMillis() - Time::getMillisecondCounter()) + (int64) ([e timestamp] * 1000.0); }


#5

It works, but I found that there is a similar call in the iPhone code:

static int64 getMouseTime (UIEvent* e) { return (int64) [e timestamp] * 1000.0; }

Where the NSEvent argument is a UIEvent instead.
I think that the body of that method should be changed too, for consistency.


#6

Already done + checked in yesterday!


#7