Trap the mouse pointer...

BlitzMax Forums/BlitzMax Beginners Area/Trap the mouse pointer...

Takis76(Posted 2015) [#1]
Hello,

I would like to ask how is it possible to trap my mouse pointer in one square area.

I tried to store the mousex() and mousey() variables in 4 separated variables
like:


Global pointer_x:Int
Global pointer_y:Int
Global pointer_w:Int
Global pointer_h:Int

pointer_x=mousex()
pointer_y=mousey()
pointer_w=mouse_x+200
pointer_h=mouse_y+100

If pointer_x<10 then pointer_x=10
If pointer_x>pointer_w then pointer_x=pointer_w
if pointer_y<10 then pointer_y=10
if pointer_y>pointer_h then pointer_y=pointer_h

DrawImage pointer, pointer_x, pointer_y,0



But it doesn't work the pointer moves in whole screen.
Is there any other command which will move the mouse in some position?

Thank you :)


Takis76(Posted 2015) [#2]
I think I made it with mousemove(x,y) function

:)


Xerra(Posted 2015) [#3]
Just hacked this out of something I wrote that did exactly that. Should be easy enough to use. I don't try and force the mouse pointer to move but just use my own image and use the HideMouse command in the code.


XMouse = MouseX() ; YMouse = MouseY()
If YMouse > 450
	YMouse = 450
EndIf

If YMouse < 100
	YMouse = 100
End If

If XMouse > 650
	XMouse = 650
EndIf

If XMouse < 1
	XMouse = 1
End If
DrawImage PlayerImage, XMouse, YMouse




Takis76(Posted 2015) [#4]
Thank you , I made it with mousemove() I would like to change the mouse position when the pointer reach these limits.


Derron(Posted 2015) [#5]
MoveMouse() moves the mouse for the whole OS ...

Xerra's suggestion is some kind of "virtual" mouse trap.


So with MoveMouse() you will not be able to get out of your windowed application. This is useful if you eg. have your mouse used for "aiming" in a 3D World - or when doing "hold mouse down to scroll" (drag n scroll - like swiping).
But if you just want your mouse to be in a specific area of your app (logic wise) you could use Xerra's approach.

But of course you should take into differences into consideration - for now the XMouse and YMouse will stay at their max a long time if you moved the mouse far to the right or bottom. Differences means to store the difference between "limit" and current position to subtract them accordingly if you move your mouse slowly back (relative position versus absolute position).


bye
Ron


Kryzon(Posted 2015) [#6]
MoveMouse causes a secondary mouse movement event. If you're enacting that rectangular constraint when you catch a mouse movement event (by using WaitEvent or event hooks), it'll lead to an infinite cycle (you will move the mouse, then process the move event, then move the mouse etc.).

In this case, in the function that processes your mouse movement event you need a boolean flag that you toggle at every call. Then only do anything in that function if the flag is True.