Limit mouse pos on screen

Just starting out? Need help? Post your questions and find answers here.
donSHAYA
User
User
Posts: 95
Joined: Wed Mar 25, 2009 9:57 am

Limit mouse pos on screen

Post by donSHAYA »

Hi all
How do you limit the area (on the opened screen) that the mouse can drive on . Like if I don't want the mouse to be able to drive on the area (x1 = 5, y1 = 5, x2 = 150, y2 = 150).
Or if I only want the mouse to be able to drive on the area (x1 = 50, y1 = 50, x2 = 600, y2 = 600).

Thank You
jamirokwai
Enthusiast
Enthusiast
Posts: 798
Joined: Tue May 20, 2008 2:12 am
Location: Cologne, Germany
Contact:

Re: Limit mouse pos on screen

Post by jamirokwai »

donSHAYA wrote:Hi all
How do you limit the area (on the opened screen) that the mouse can drive on . Like if I don't want the mouse to be able to drive on the area (x1 = 5, y1 = 5, x2 = 150, y2 = 150).
Or if I only want the mouse to be able to drive on the area (x1 = 50, y1 = 50, x2 = 600, y2 = 600).

Thank You
Hi,

if you draw a Sprite at the mouse position, you could say (just a dirty hack without having PB installed here...)

Code: Select all

 x = MouseX() 
 y = MouseY()
 If x < 50 : x = 50 : endif
 if x > 600 : x = 600 : endif
 If y < 50 : y = 50 : endif
 if y > 600 : y = 600 : endif
 DrawTransparentSprite(0,x,y)
This is how I did it in Borland Pascal for Dos :)
Regards,
JamiroKwai
User avatar
Kaeru Gaman
Addict
Addict
Posts: 4826
Joined: Sun Mar 19, 2006 1:57 pm
Location: Germany

Re: Limit mouse pos on screen

Post by Kaeru Gaman »

almost
... relocating would be good

Code: Select all

x = MouseX()
y = MouseY()
If x < 50 : x = 50 : endif
if x > 600 : x = 600 : endif
If y < 50 : y = 50 : endif
if y > 600 : y = 600 : endif
MouseLocate(x,y)
DisplayTransparentSprite(0,x,y)
otherwise the mouse pointer could show a jumpy behaviour.
oh... and have a nice day.
User avatar
einander
Enthusiast
Enthusiast
Posts: 744
Joined: Thu Jun 26, 2003 2:09 am
Location: Spain (Galicia)

Re: Limit mouse pos on screen

Post by einander »

This is for trap Mouse inside a window region.
May be you can adapt it for screen.

Code: Select all

Procedure MouseTrap(X,Y,Wi,He)
   GetWindowInfo_(WindowID(EventWindow()),@P.WindowInfo)
   WX=P\RcClient\Left
   Wy=P\RcClient\Top
   SetRect_(R.RECT,WX+X,Wy+Y,WX+X+Wi,Wy+Y+He)
   ClipCursor_(@R)  
EndProcedure
   
OpenWindow(0, 0, 0, 600, 600, "Mouse Trap - <ESC> to close window", 1) 
   
Btn1=ButtonGadget(-1,200,100,200,200,"QUIT")
   
Repeat
   Ev = WaitWindowEvent()
   If GetAsyncKeyState_(27)&$8000 :  End : EndIf
   MouseTrap(200,100,200,200)
   
Until Ev = #PB_Event_Gadget And EventGadget()=Btn1
End 
donSHAYA
User
User
Posts: 95
Joined: Wed Mar 25, 2009 9:57 am

Re: Limit mouse pos on screen

Post by donSHAYA »

Thanks for all replies.

I used Kaeru Gaman's example because it worked while OpenScreen. The mousetrap didn't work while OpenScreen.

But thank you for every reply. very helpful
Post Reply