Sign in to follow this  
Followers 0

Restrict Mouse Movement - Circle round middle of screen

9 posts in this topic

Posted (edited)

How to restrict/limit the mouse movement to a circle area around the middle of the screen?

This will be very usefull <snip>.

I would be VERY grateful for an answer! :bye:

Edited by Melba23
Edited text

Share this post

Link to post
Share on other sites



Take a look at the function _MouseTrap

Br, FireFox.

Share this post

Link to post
Share on other sites


Thank you for the fast answer! I understand how this code work, but I dont understand how to trap it in a circle. Is it possible?

#include <GUIConstantsEx.au3>
#include <Misc.au3>


Func _Main()
Local $GUI, $coords[4]

$GUI = GUICreate("Mouse Trap Example", 392, 323)


While 1
$coords = WinGetPos($GUI)
_MouseTrap($coords[0], $coords[1], $coords[0] + $coords[2], $coords[1] + $coords[3])
Switch GUIGetMsg()
Case Else
EndFunc   ;==>_Main

Share this post

Link to post
Share on other sites



I have edited your first post. The question is interesting in itself (which is why I am leaving the thread open), but you did not need to specify the usage to which you want to put it - that was not a good idea. :naughty:

Please read the Forum Rules before you post again - you do not get the same treatment if you do it a second time. ;)


Share this post

Link to post
Share on other sites


Haven't had time to work out the math but what you could do is use mousetrap to define the outside box of a circle. Anything that is outside the circle you want but inside the box could be handled by saving the "old" mouse coords and capturing the "new" ones. Compare the new ones to the result of the equation for a circle and if they are outside the circle then move the mouse back to the "old" coords.

Equation for a circle with any center point is: (x-h)2 + (y-k)2 = r2


x is the X coord of the mouse

y is the Y coord of the mouse

h is the X coord of circle center

k is the Y coord of circle center

r is the radius of a circle

You may need to rearrange the equation to make it easier to use to test against.

So basically plug in the X,Y coords of the mouse position and if the calculated "r" is less than the radius of the circle you want to restrict to then use the new coords, otherwise move mouse back to the old coords.

One problem I could see the mouse might be a little jumpy depending on how fast the user is moving it outside of the circle although the mousetrap will help limit it some.

Share this post

Link to post
Share on other sites

Posted (edited)

Here's some quick code I came up with that works but does have some limitations.

You can hit the {ESC} to exit the script and reset the _MouseTrap to normal

#include <Misc.au3>

HotKeySet("{ESC}", "Terminate")

Local $XCenter = Int(@DesktopWidth/2)
Local $YCenter = Int(@DesktopHeight/2)
Local $OldMouse
Local $NewMouse

If $XCenter < $YCenter Then
     Local $Radius = Int($XCenter/2)
     Local $Radius = Int($YCenter/2)

_MouseTrap(($XCenter-$Radius), ($YCenter - $Radius), ($XCenter + $Radius), ($YCenter + $Radius))

$OldMouse = MouseGetPos()

While 1

$NewMouse = MouseGetPos()

If Int(Sqrt((($NewMouse [0] - $XCenter)^2)+(($NewMouse [1] - $YCenter)^2))) > $Radius Then
MouseMove($OldMouse [0], $OldMouse [1], 1)
$OldMouse = $NewMouse


Func Terminate()
Exit 0
EndFunc ;==>Terminate

Main thing is that if you have a lot going on in the While/Wend loop it does get jumpy as the mouse can move outside of the circle before it gets a chance to read the new position and fix it.

Here's a picture I made in paint with the program running. You can see where even though I only had mouse position checking in my loop, it would jump outside the circle long enough for it to paint if I made a fast sharp movement outside before it corrected it and moved it back. If you move the mouse at a moderate speed though it does a pretty good job of keeping it inside with out the jerkiness.

I'm sure someone else can come up with something that works a little better.

Not sure but if you look at the _MouseTrap function code in the #include you can see where they use DLL's to limit mouse position so it may be possible to mod that to fit your needs instead.

Hope this helps


Edited by wyzzard

Share this post

Link to post
Share on other sites



This i very specific about the dll call parameter being a rectangle. Good thought, though!


Share this post

Link to post
Share on other sites


You need to use a mouse hook and then you won't get any movement outside of the circle.

here is a quick mod I made to a mousehook example just to show you.

#region ;**** Directives created by AutoIt3Wrapper_GUI ****
#endregion ;**** Directives created by AutoIt3Wrapper_GUI ****
;~~ Mouse Hook ~~
;For more info, Visit:

;Include GUI Consts
#include <GUIConstants.au3> ;for $GUI_EVENT_CLOSE
#include <WinAPI.au3> ;for HIWORD

#region bits for the circle trapping
HotKeySet("{ESC}", "Terminate")

Local $XCenter = Int(@DesktopWidth / 2)
Local $YCenter = Int(@DesktopHeight / 2)

If $XCenter < $YCenter Then
    Local $Radius = Int($XCenter / 2)
    Local $Radius = Int($YCenter / 2)
MouseMove($XCenter, $YCenter) ;start with the cursor inside the circle or it will never get there
$RadSq = $Radius * $Radius ;so we can calculate a bit faster and not do sq roots every time
#endregion bits for the circle trapping

;These constants found in the helpfile under Windows Message Codes
Global Const $WM_MOUSEMOVE = 0x0200 ;mouse move
Global Const $WM_MOUSEWHEEL = 0x020A ;wheel up/down
Global Const $WM_LBUTTONDBLCLK = 0x0203 ;left button
Global Const $WM_LBUTTONDOWN = 0x0201
Global Const $WM_LBUTTONUP = 0x0202
Global Const $WM_RBUTTONDBLCLK = 0x0206 ;right button
Global Const $WM_RBUTTONDOWN = 0x0204
Global Const $WM_RBUTTONUP = 0x0205
Global Const $WM_MBUTTONDBLCLK = 0x0209 ;wheel clicks
Global Const $WM_MBUTTONDOWN = 0x0207
Global Const $WM_MBUTTONUP = 0x0208

;Consts/structs from msdn
Global Const $MSLLHOOKSTRUCT = $tagPOINT & ";dword mouseData;dword flags;dword time;ulong_ptr dwExtraInfo"
;~ Global Const $WH_MOUSE_LL = 14          ;already declared
;~ Global Const $tagPOINT = "int X;int Y"   ;already declared

;Create GUI
$GUI = GUICreate("Mouse Hook", 178, 158, @DesktopWidth - 178, 0) ;Top-Left corner
$_Event = GUICtrlCreateLabel("Event: ", 8, 8, 158, 17)
$_XYpos = GUICtrlCreateLabel("X=     Y=", 8, 32, 157, 17)
$_MData = GUICtrlCreateLabel("Mouse Data: ", 8, 56, 165, 17)
$_Flags = GUICtrlCreateLabel("Flags: ", 8, 80, 168, 17)
$_Timestamp = GUICtrlCreateLabel("Timestamp: ", 8, 104, 162, 17)
$_Extra = GUICtrlCreateLabel("Extra Info: ", 8, 128, 167, 17)
WinSetOnTop($GUI, "", 1) ;make GUI stay on top of other windows

;Register callback
$hKey_Proc = DllCallbackRegister("_Mouse_Proc", "int", "int;ptr;ptr")
$hM_Module = DllCall("kernel32.dll", "hwnd", "GetModuleHandle", "ptr", 0)
$hM_Hook = DllCall("user32.dll", "hwnd", "SetWindowsHookEx", "int", $WH_MOUSE_LL, "ptr", DllCallbackGetPtr($hKey_Proc), "hwnd", $hM_Module[0], "dword", 0)

While 1
    If $GUI_EVENT_CLOSE = GUIGetMsg() Then Exit ;idle until exit is pressed

Func _Mouse_Proc($nCode, $wParam, $lParam) ;function called for mouse events..
    ;define local vars
    Local $info, $ptx, $pty, $mouseData, $flags, $time, $dwExtraInfo, $Res
    Local $xevent = "Unknown", $xmouseData = ""

    If $nCode < 0 Then ;recommended, see
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
                "int", $nCode, "ptr", $wParam, "ptr", $lParam) ;recommended
        Return $ret[0]

    $info = DllStructCreate($MSLLHOOKSTRUCT, $lParam) ;used to get all data in the struct ($lParam is the ptr)
    ; DllStructGetData($info, 1) ;see notes below..
    $ptx = DllStructGetData($info, 1)
    $pty = DllStructGetData($info, 2)
    DllStructSetData($info, 2, 100)
    $mouseData = DllStructGetData($info, 3)
    $flags = DllStructGetData($info, 4)
    $time = DllStructGetData($info, 5)
    $dwExtraInfo = DllStructGetData($info, 6)
    ; $ptx = Mouse x position
    ; $pty = Mouse y position
    ; $mouseData = can specify click states, and wheel directions
    ; $flags = Specifies the event-injected flag
    ; $time = Specifies the time stamp for this message
    ; $dwExtraInfo = Specifies extra information associated with the message.

    ;Find which event happened
    ConsoleWrite($ptx & ', ' & $pty & @LF)
    $Res = 0
        Case $wParam = $WM_MOUSEMOVE
            $xevent = "Mouse Move"
            If ($ptx - $XCenter) ^ 2 + ($pty - $YCenter) ^ 2 > $RadSq Then
                $Res = 1
        Case $wParam = $WM_MOUSEWHEEL
            $xevent = "Mouse Wheel"
            If _WinAPI_HiWord($mouseData) > 0 Then
                $xmouseData = "Wheel Forward"
                $xmouseData = "Wheel Backward"
        Case $wParam = $WM_LBUTTONDBLCLK;double clicks don't seem to work so we would need a timer to decide. There must be a time set in spi or somewhere which windows uses.
            $xevent = "Double Left Click"
            ConsoleWrite("doubled" & @CRLF)
        Case $wParam = $WM_LBUTTONDOWN
            $xevent = "Left Down"
        Case $wParam = $WM_LBUTTONUP
            $xevent = "Left Up"
        Case $wParam = $WM_RBUTTONDBLCLK
            $xevent = "Double Right Click"
        Case $wParam = $WM_RBUTTONDOWN
            $xevent = "Right Down"
        Case $wParam = $WM_RBUTTONUP
            $xevent = "Right Up"
        Case $wParam = $WM_MBUTTONDBLCLK
            $xevent = "Double Wheel Click"
        Case $wParam = $WM_MBUTTONDOWN
            $xevent = "Wheel Down"
        Case $wParam = $WM_MBUTTONUP
            $xevent = "Wheel Up"

    ; Set GUI control data..
    GUICtrlSetData($_Event, "Event: " & $xevent)
    GUICtrlSetData($_XYpos, "X=" & $ptx & "  Y=" & $pty)
    If $xmouseData <> "" Then
        GUICtrlSetData($_MData, "Mouse Data: " & $xmouseData)
        GUICtrlSetData($_MData, "Mouse Data: " & $mouseData)
    GUICtrlSetData($_Flags, "Flags: " & $flags)
    GUICtrlSetData($_Timestamp, "Timestamp: " & $time)
    GUICtrlSetData($_Extra, "Extra Info: " & $dwExtraInfo)

    ;This is recommended instead of Return 0
    If $Res = 1 Then
        Return 1
        $ret = DllCall("user32.dll", "long", "CallNextHookEx", "hwnd", $hM_Hook[0], _
                "int", $nCode, "ptr", $wParam, "ptr", $lParam)
        Return $ret[0]
EndFunc   ;==>_Mouse_Proc

Func terminate()
EndFunc   ;==>terminate

Func OnAutoItExit()
    DllCall("user32.dll", "int", "UnhookWindowsHookEx", "hwnd", $hM_Hook[0])
    $hM_Hook[0] = 0
    $hKey_Proc = 0
EndFunc   ;==>OnAutoItExit

Share this post

Link to post
Share on other sites


Tested martin's version and it definitely does a better job keeping it inside the circle. Not sure if it was my mouse or not but it seemed a little less responsive sometimes (in particular larger moves towards the edge of the circle) but that could be my perception of things.

Attached a picture of it in action though


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0

  • Recently Browsing   0 members

    No registered users viewing this page.