Quantcast
Channel: AutoHotkey Community
Viewing all articles
Browse latest Browse all 579

Ask for Help (v1) • Re: Trying to make a simple script that uses DLLCall mouse_event to move the cursor to sets of coordiantes

$
0
0

Hallo,
try:

CODE:

; Function to move the mouse cursor to a specific position on the screen
MoveMouseToAbsolute(x, y) {
Static SysX := 0x10000/A_ScreenWidth, SysY := 0x10000/A_ScreenHeight

; Calculate the absolute coordinates
xa := Ceil(x * SysX)
ya := Ceil(y * SysY)

; Move the mouse using mouse_event function, 0x8001: ABSMOVE
DllCall("mouse_event", "UInt", 0x8001, "UInt", xa, "UInt", ya)

; Show tooltip with current and target coordinates
Tooltip, Target coordinates:`nX: %x%`nY: %y%

; Sleep to allow time for the mouse cursor to move (adjust as needed)
Sleep 1000

; Clear tooltip
Tooltip
}

; Example: Move mouse to several positions
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)
Sleep 1000
MoveMouseToAbsolute(1280, 720)

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp




Brilliant, that worked quite well. Thank you! Was this because you had set absolute screen dimensions?

I'm trying to get it to read a txt file that has object coordinates (x, y) where the mouse is supposed to move to, but I keep getting an error that I can't seem to resolve. It was working previously when I was using a different mouse event function, but I can't imagine how that would be interfering in this.

CODE:

#NoEnv
#SingleInstance, Force
#Persistent
#InstallKeybdHook
#UseHook
#KeyHistory, 0
#HotKeyInterval 1
#MaxHotkeysPerInterval 127

SetTitleMatchMode, 2 ; Allow for partial title matches

; Function to move the mouse cursor to a specific position on the screen
MoveMouseToAbsolute(x, y) {
Static SysX := 0x10000/A_ScreenWidth, SysY := 0x10000/A_ScreenHeight

; Calculate the absolute coordinates
xa := Ceil(x * SysX)
ya := Ceil(y * SysY)

; Move the mouse using mouse_event function, 0x8001: ABSMOVE
DllCall("mouse_event", "UInt", 0x8001, "UInt", xa, "UInt", ya)

; Show tooltip with current and target coordinates
Tooltip, Target coordinates:`nX: %x%`nY: %y%

; Sleep to allow time for the mouse cursor to move (adjust as needed)
Sleep 1000

; Clear tooltip
Tooltip
}

; Main loop
Loop {
; Read the content of the input file
FileRead, ObjectData, F:\object_data.txt

; Get the current mouse position
MouseGetPos, MouseX, MouseY

; Split the content by lines
Loop, Parse, ObjectData, `n, `r {
; Split the line by comma to extract coordinates
CoordArray := StrSplit(A_LoopField, ",")

; Check if the array contains at least 2 elements (x, y)
if (CoordArray.Length() >= 2) {
; Extract coordinates
center_x := CoordArray[1]
center_y := CoordArray[2]

; Show tooltip with target and current mouse coordinates
ToolTip, Target coordinates:`nX: %center_x%`nY: %center_y%`nCurrent Mouse:`nX: %MouseX%`nY: %MouseY%

; Move the mouse cursor to the extracted coordinates
MoveMouseToAbsolute(center_x, center_y)
}
}
; Sleep to avoid high CPU usage
Sleep 100
}

return

; Hotkeys for Control
Pause:: Pause
^Esc:: ExitApp

Statistics: Posted by prototype_zero — Today, 14:56



Viewing all articles
Browse latest Browse all 579

Trending Articles