Mouse tracking Script

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GAMEDOGSHIPMENT
Posts: 1
Joined: 08 Mar 2023, 13:35

Mouse tracking Script

08 Mar 2023, 15:22

Hello, I am new to this forum and forgive me if the format is not up to spec or if this has been answered already.

I have been playing around with the tracking script. I want to make a script that searches on my screen 1 or 2 areas, then after a few seconds my mouse is moved but I want to keep the screen in place by sending mouse movements to return to where it belongs. As well there might be some deviation of course since it's not on a constant environment so the areas will change ever so slightly.

I came up with this but I have not as much experience so the script is not exactly working. Any help would be appreciated.

EDIT: There was some code correction needed, so reposted

Code: Select all

; Define the areas to track
Area1 := {X: 100, Y: 200, Width: 50, Height: 50}
Area2 := {X: 300, Y: 400, Width: 75, Height: 75}

; Set up the toggle key
ToggleKey := "F2"
Tracked := False

; Set up the script loop
Loop
{
    ; Check if the toggle key is pressed
    if GetKeyState(ToggleKey, "P")
    {
        ; Toggle the tracking
        Tracked := !Tracked
    }
    
    ; If tracking is enabled, move the mouse to keep the areas in place
    if Tracked
    {
        ; Get the current mouse position
        MouseX := A_CursorX
        MouseY := A_CursorY
        
        ; Check if the mouse is within one of the tracked areas
        if InArea(MouseX, MouseY, Area1)
        {
            ; Mouse is within Area1, move it to keep the area in place
            MouseMove(Area1.X + (Area1.Width / 2), Area1.Y + (Area1.Height / 2))
        }
        else if InArea(MouseX, MouseY, Area2))
        {
            ; Mouse is within Area2, move it to keep the area in place
            MouseMove(Area2.X + (Area2.Width / 2), Area2.Y + (Area2.Height / 2))
        }
    }
    
    ; Wait for 10 milliseconds before looping again
    Sleep, 10
}

; Function to check if a point is within an area
InArea(x, y, area)
{
    return (x >= area.X && x <= area.X + area.Width && y >= area.Y && y <= area.Y + area.Height)
}

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: AlFlo and 146 guests