How to get the delta of the mouse movement?

I need to get the delta of the mouse movement (the change of the X and Y coordinates when the user moves the mouse). I don’t want to know the X and Y coordinates of the pointer itself, as the pointer can get stuck at the left screen frame (x=0), for example. I need to know how far has the user moved the mouse, regardless of the real position of the pointer.

I hope my explanation is understandable, I was doing my best :wink:

Any idea how can I do it in Xojo? Mac only.

make 4 Properties

oldX As Integer
oldY As Integer
newX As Integer
newY As Integer

MouseDown:

oldX = X oldY = Y main.Title = "0 / 0"

MouseMove: or MouseUp ( then add Return True in MouseDown)

newX = X newY = Y main.Title = str(newX - oldX) + " / " + str(newY - oldY)

When just moving it ?

In mouse move

  static oldX As Integer
  static oldY As Integer
  dim deltaX as integer = X - oldX
  dim deltaY as integer = Y - oldY
  system.debuglog str(deltaX) + " / " + str(deltaY
  oldX = X
  oldY = Y

replace statics with 2 integer properties if you’re doing this in a subclass

But I need it to work beyond the constraints of a window or even the screen.

Like in a game such as Witcher or Deus Ex, for example: you want to look around, so you move the mouse left, therefore the camera goes around and does not stop. What I need is a delta_x and delta_y of the mouse “raw” movement – not depending on x and y coordinates of the pointer.

The goal is to get the distance the mouse was moved even if the pointer itself reached the border of the screen.

Oh you want the raw events from the device not what it gets translated into in logical coordinates on screen
Not sure you can hook into that level of event from the device
Declares will be required

Could you use System.MouseX/Y? http://documentation.xojo.com/index.php/System.MouseX

that only reports it having actually moved the mouse pointer

sounds like he wants the low level event where the person tries to move left but the mouse pointer is already at x = 0 so there wont be any additional changes
but the very low level mouse events are still sent to the OS

just dont know if you can hook into that very low level event queue

Shouldn’t a GameInputDevice be able to catch mouse events?

Getting to the hardware at OSI layer 4 or below may be not only challenging, but also rather fragile. Who knows how many different mice and trackpads can be plugged in ? Through which interfaces, between Blue Tooth and USB ?

On a multi-screen system, you get several screens to play on. But it remains limited.

Why not instead paginate the cursor ?

When the cursor MouseX gets to zero, move the pointer to screen(0).Width, so you get another screen width to go through. You can get in effect an unlimited space to track the mouse on.

It will require some work on edge cases when cursor starts at 10 and ends at Screen(0).Width-50 for instance, which would be a total delta of 60, but it seems feasible.

Check https://forum.xojo.com/12285-move-mouse-cursor-and-click-by-code-for-mac/0 for the mouse cursor displacement.

Of course, that idea stands only if in fact you don’t care when the mouse cursor stands, but that is implied by your description of what you want to achieve.

I’ve done this before using just CGWarpMouseCursorPosition to keep the cursor in the middle of the screen, but trying that now is killing any delta.

Look into Controlling the Mouse Cursor for other functions. Warp + CGGetLastMouseDelta does the trick, but I think you could also use CGAssociateMouseAndMouseCursorPosition(false) and then in the mouse event get the ‘real’ event from the OS where you can find the delta.

Here’s Warp + CGGetLastMouseDelta in a Windows MouseMove event, though it works in a controls MouseMove or MouseDrag too. In a MouseMove you want to make sure the cursor is warped within the window/control.

[code]Structure CGPoint
x As CGFloat
y As CGFloat

Sub MouseMove(X As Integer, Y As Integer)

declare sub CGGetLastMouseDelta lib “CoreGraphics” (byref x As Int32, byref y As Int32)
declare function CGWarpMouseCursorPosition lib “CoreGraphics” (newPos As CGPoint) As Int32

dim dx, dy As Int32

CGGetLastMouseDelta(dx, dy)

self.Title = Str(dx) + ", " + Str(dy)

dim p As CGPoint
p.x = self.Left + self.Width / 2
p.y = self.Top + self.Height / 2

dim e As Int32 = CGWarpMouseCursorPosition§

End Sub[/code]

Also, CGGetLastMouseDelta returns deltas even when the cursor is pinned to the edge. So if you’re in Fullscreen and disabled the titlebar from dropping down (is that doable?) then you don’t need to warp.

That’s what I was looking for. Thank you!