Need help automating a script for mouseclickdrag in a game

Ask gaming related questions (AHK v1.1 and older)
vikman9158
Posts: 4
Joined: 12 May 2022, 14:02

Need help automating a script for mouseclickdrag in a game

Post by vikman9158 » 12 May 2022, 14:07

I can't for the life of me figure out how to execute a script so that it will activate a game window and then click and drag the mouse. Moving items between inventories is very tedious in this game and i very badly need to learn how to automate this. I've only gotten this far

Code: Select all

#NoEnv
			
SetWorkingDir D:\Downloads\AHK_SCRIPTS-master
			
CoordMode, Mouse, Screen
			
SendMode Input
			
#SingleInstance Force
			
SetTitleMatchMode 2
			
WinActivate ahk_exe XXXXXX.exe

MouseClickDrag, Left, 1850, 358, 355, 647

sleep 100
Esc:: ExitApp
		
ExitApp
[Mod edit: [code][/code] tags added.]

So far whenever I run the script it activates the window that my game is running on but doesn't do anything else. Any command I put after the Winactivate doesn't seem to work. My idea was just to copy and paste the mouseclickdrag command 100 times to simplify the script for me but if anybody knows how to do a hotkey version where it will start the mouseclickdrag function when pressing ALT-P and keep doing it until i press ALT-Y, that would be even more amazing. Really appreciate the help.

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Need help automating a script for mouseclickdrag in a game

Post by mikeyww » 12 May 2022, 14:46

Welcome to this AutoHotkey forum!

Start with something more basic: see if you can move the mouse.

Code: Select all

winTitle = ahk_exe notepad.exe
#If WinExist(winTitle)
F3:: ; F3 = Move the mouse
WinActivate
MouseMove, 200, 200
Return
#If
If it does not work, then try running the script as admin.

vikman9158
Posts: 4
Joined: 12 May 2022, 14:02

Re: Need help automating a script for mouseclickdrag in a game

Post by vikman9158 » 14 May 2022, 04:55

Wow thanks that helped a lot. Can you help explain why your way of scripting actually allowed the action to perform in-game. The way i had it before it worked in paint or Chrome but just wouldn't function in the game I was using it for. I've also redone the script a bit since I felt it worked slightly better.

Code: Select all

#NoEnv
#Warn
SetWorkingDir %A_ScriptDir%
SendMode, Input

SetMouseDelay 20
SetMouseDelay 20, Play

winTitle = ahk_exe StarCitizen.exe
#If WinExist(winTitle)
#MaxThreadsPerHotkey 1
F5:: ; F5 = Move the mouse left to right
WinActivate
SendEvent {Click 160 326 Down}{Click 1964 1034 Up}

Sleep, 50
Return

F6:: ; F6 = Move the mouse left to right
WinActivate
SendEvent {Click 1834 334 Down}{Click 343 923 Up}

Sleep, 50
#If

#IfWinActive

THe sendevent seems to work better than mouseclickdrag function. I noticed a problem in-game cause the mouse still moves to fast. I suppose just increasing the setmousedelay should solve that issue right? I also would like to add a loop to this so after I press the hotkey, the script will run indefinitely until I press another button to stop it. I tried adding 

Loop, 3
{ 
                        SendEvent {Click 160 326 Down}{Click 1964 1034 Up}
                        Sleep, 50
                        Return
}
but it doesn't do it three times. I wanted to first try it looping it with a set variable before trying to figure out how to do the indefinite function, but wouldn't mind jumping to the indefinite scripting part.


[Mod edit: [code][/code] tags added. Please use them yourself when posting code.]

User avatar
mikeyww
Posts: 26939
Joined: 09 Sep 2014, 18:38

Re: Need help automating a script for mouseclickdrag in a game

Post by mikeyww » 14 May 2022, 05:19

I'm not sure why your original script did not work. Some ideas are below.

Code: Select all

#MaxThreadsPerHotkey 2

winTitle = ahk_exe StarCitizen.exe

#If WinExist(winTitle)
F6:: ; F6 = Move the mouse left to right
If on := !on
 WinActivate
While on {
 Send {Click 1834 334 Down}
 MouseMove, 343, 923, SLOW := 9
 Send {Click Up}
}
Return
#If

vikman9158
Posts: 4
Joined: 12 May 2022, 14:02

Re: Need help automating a script for mouseclickdrag in a game

Post by vikman9158 » 16 May 2022, 10:34

Hi Mikey. After using your revised script, when I run it, it activates the game window but nothing happens. When I alt tab out of the window, i find that my mouse is doing the click and drag function. Any idea why its not doing it in game?

vikman9158
Posts: 4
Joined: 12 May 2022, 14:02

Re: Need help automating a script for mouseclickdrag in a game

Post by vikman9158 » 16 May 2022, 10:49

nvm i forgot i need to run as administrator.....

Thanks mike! you've been a huge help

Post Reply

Return to “Gaming Help (v1)”