Trying to make a simple script that uses DLLCall mouse_event to move the cursor to sets of coordiantes Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
prototype_zero
Posts: 17
Joined: 19 Mar 2024, 13:01

Trying to make a simple script that uses DLLCall mouse_event to move the cursor to sets of coordiantes

23 Mar 2024, 15:55

Hello,

I've used different mouse functions that worked fine and in a very straight forward way of simply moving the mouse to a set of absolute coordinates on the screen. But for some reason, as soon as I use DLLCall mouse_event, the mouse cursor, regardless of what the target coordinates are, just bounces around from one corner to the other. Even if I set the coordinates all to the same 1280,720, which in theory would mean the mouse cursor would remain stationary, it still continues to bounce around. Any help on this would be appreciated.

Code: Select all

; Function to move the mouse cursor to a specific position on the screen
MoveMouseToAbsolute(x, y) {
    ; Get the current mouse position
    MouseGetPos, startX, startY
    
    ; Calculate the distance to move
    dx := x - startX
    dy := y - startY
    
    ; Move the mouse using mouse_event function
    DllCall("mouse_event", UInt, 1, Int, dx, Int, dy, UInt, 0, UInt, 0)
    
    ; 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


Rohwedder
Posts: 7732
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Trying to make a simple script that uses DLLCall mouse_event to move the cursor to sets of coordiantes  Topic is solved

24 Mar 2024, 03:38

Hallo,
try:

Code: Select all

; 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

prototype_zero
Posts: 17
Joined: 19 Mar 2024, 13:01

Re: Trying to make a simple script that uses DLLCall mouse_event to move the cursor to sets of coordiantes

24 Mar 2024, 22:11

Rohwedder wrote:
24 Mar 2024, 03:38
Hallo,
try:

Code: Select all

; 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, thank you very much. It worked perfectly.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot] and 129 guests