mousedrag accuracy

I have a canvas with the following code in the mousedrag event. If I drag somewhat quickly outside the specified range, the value I get almost always different. I would like to be able to exit upward in the canvas so that y will be at 0 or downward in the canvas so that y will be at the height of the canvas. Is there a way to get accurate drag information?

[code] if y >= 0 and y <= me.Height then

TextField1.Text = str(y)

end if[/code]

Why not use the MouseExit event to determine if the pointer has left the canvas?

Save the last Y location and catch it in MouseExit.

dim y as integer
if LastY < me.Height/2 then
   y = 0
else
  y = me.Height
end
TextField1.Text = str(y)

[quote=88541:@Joseph Morgan]I have a canvas with the following code in the mousedrag event. If I drag somewhat quickly outside the specified range, the value I get almost always different. I would like to be able to exit upward in the canvas so that y will be at 0 or downward in the canvas so that y will be at the height of the canvas. Is there a way to get accurate drag information?

[code] if y >= 0 and y <= me.Height then

TextField1.Text = str(y)

end if[/code][/quote]

The problem here is the mouse sampling resolution. The MouseDrag even event is called approximatively every 33500 microseconds. When you move the mouse too quickly, the system does not have time to catch all the mouse positions. This limit is imposed largely by the hardware and there is no software workaround.

Use MouseExit to record the “0” and “Me.Height” exit. You can record the last position of the mouse with a window property you set in MouseDrag, and for instance set it as follow in MouseExit :

if lastmouseY <70 then lastmouseY = 0 elseif lastmouseY > me.Height-70 then lastmouseY = me.Height end if

This way you know if the mouse exited on top or bottom.

Clamp the coordinate first…

[code]Sub MouseDrag(X As Integer, Y As Integer)
if y < 0 then y = 0
if y > me.Height then y = me.Height

TextField1.Text = Str(y)
End Sub[/code]

[quote=88593:@Will Shank]Clamp the coordinate first…

[code]Sub MouseDrag(X As Integer, Y As Integer)
if y < 0 then y = 0
if y > me.Height then y = me.Height

TextField1.Text = Str(y)
End Sub[/code][/quote]

y cannot be negative or exceed the height. It is set relative to the canvas boundaries… If the mouse exits the canvas, MouseDrag stops firing.

I think you’re thinking of MouseMove. MouseDrag doesn’t fire until you return true in MouseDown and then continues to fire until MouseUp, even if the cursor leaves the canvas.

Coordinates given during MouseDrag are relative but can be relative anywhere on screen. Also, I seem to remember that MouseExit/Enter stopped during MouseDrags but I see now that a single MouseExit will fire while dragging, but no more and no Enters.

[quote=88614:@Will Shank]I think you’re thinking of MouseMove. MouseDrag doesn’t fire until you return true in MouseDown and then continues to fire until MouseUp, even if the cursor leaves the canvas.

Coordinates given during MouseDrag are relative but can be relative anywhere on screen. Also, I seem to remember that MouseExit/Enter stopped during MouseDrags but I see now that a single MouseExit will fire while dragging, but no more and no Enters.[/quote]

You are right, MouseDrag does produce negative coordinates and exceeds the control height. But still the code I posted will produce results in MouseExit without the clamping :

if lastmouseY <70 then lastmouseY = 0 elseif lastmouseY > me.Height-70 then lastmouseY = me.Height end if

And MousExit works fine with MouseDrag, once only as you say.

Whatever the OP intended to do, he now as a full range of possibilities :wink:

Thank you all for the suggestions. Eventually the area I want to drag inside of will be smaller than me.height but I wanted to get at least that working before I move forward.