# If Mouse Moves More Than

## Recommended Posts

I'm trying to figure out how to do something when the mouse moves more than a given distance in any direction.

pseudo-code:

```\$pos = MouseGetPos(); Get initial mouse pos
\$give0 = \$pos[0] - 5 to \$pos[0] + 5  ;have a little give that the mouse can move
\$give1 = \$pos[1] - 5 to \$pos[1] + 5  ;in both axis

While \$pos2[0]= \$give0 & \$pos2[1] = \$give1   ;keep recording where mouse is and while it's in the space given keep recording and checking
\$pos2 = MouseGetPos()
Sleep(100)
WEnd

MsgBox(0, "", "Mouse mooved Too Far") ;You blew it.. It's outside of the area given.```

Is there a better way to do this? Correction, what is a better way to do this?

##### Share on other sites

This is how I would do it.

```\$Pos = MouseGetPos(); Get initial mouse pos

Dim \$Pos2=\$Pos

While Abs(\$Pos[0]-\$Pos2[0])<=5 and Abs(\$Pos[1]-\$Pos2[1])<=5
\$Pos2 = MouseGetPos()
Sleep(100)
WEnd

MsgBox(0, "", "Mouse moved Too Far")```

Using ABS to get the absolute value so you can check that its less than or equal to 5 in both directions for each axis.

HKTunes:Softpedia | GoogleCodeLyricToy:Softpedia | GoogleCodeRCTunes:Softpedia | GoogleCodeMichtaToolsProgrammer n. - An ingenious device that turns caffeine into code.

##### Share on other sites

This is how I would do it.

```\$Pos = MouseGetPos(); Get initial mouse pos

Dim \$Pos2=\$Pos

While Abs(\$Pos[0]-\$Pos2[0])<=5 and Abs(\$Pos[1]-\$Pos2[1])<=5
\$Pos2 = MouseGetPos()
Sleep(100)
WEnd

MsgBox(0, "", "Mouse moved Too Far")```

Using ABS to get the absolute value so you can check that its less than or equal to 5 in both directions for each axis.

Hey thanks for the quick reply! I'll play around with this, thanks again!

##### Share on other sites

```\$distance = 50

\$startX = MouseGetPos(0)
\$startY = MouseGetPos(1)

While Sqrt(Abs(MouseGetPos(0) - \$startX) ^ 2 + Abs(MouseGetPos(1) - \$startY) ^ 2) < \$distance
Sleep(10)
WEnd

MsgBox(0, "", "Mouse moved beyond " & \$distance & " pixels.")```

With GUI:

```#include <WindowsConstants.au3>
#include <GDIPlus.au3>
Global \$distance = 150; Edit this for a different distance trigger
Global \$startX = MouseGetPos(0), \$startY = MouseGetPos(1)
_Main()
While Sqrt(Abs(MouseGetPos(0) - \$startX) ^ 2 + Abs(MouseGetPos(1) - \$startY) ^ 2) < \$distance
Sleep(10)
WEnd

GUISetState(@SW_HIDE, \$hGUI)
_GDIPlus_Shutdown ()
MsgBox(0, "", "Mouse moved beyond " & \$distance & " pixels.")

Func _Main()
Local \$hGUI, \$hWnd, \$hGraphic

; Create GUI
Global \$hGUI = GUICreate("GDI+", \$distance * 2 + 1, \$distance * 2 + 1, \$startX - \$distance, \$startY - \$distance, \$WS_POPUP,BitOR(\$WS_EX_TOPMOST, \$WS_EX_TOOLWINDOW))
\$hWnd = WinGetHandle("GDI+")
GUISetState()

; Draw an ellipse
_GDIPlus_Startup ()
\$hGraphic = _GDIPlus_GraphicsCreateFromHWND (\$hWnd)
\$hBrush = _GDIPlus_BrushCreateSolid( 0xFF00FF00 )
_GDIPlus_GraphicsFillRect(\$hGraphic, 0, 0, \$distance * 2, \$distance * 2, \$hBrush)
\$hPen = _GDIPlus_PenCreate( 0xFFFF0000, 2)
_GDIPlus_GraphicsDrawEllipse(\$hGraphic, 0, 0, \$distance * 2, \$distance * 2, \$hPen)

EndFunc   ;==>_Main```
Edited by danwilli

## Create an account

Register a new account