AHK: How to Automate Mouse Movements

Automating mouse drags is… Not as useful as I expected. But wildly helpful if you need to draw the same thing on a computer repeatedly. Keep in mind, it only works with straight lines. Quite wonderfully, you can control that line entirely down to the pixel.

Hello

Important Commands to Note:

MouseClickDrag, Right or Left Button Mouse Button, xStart, yStart, xEnd, yEnd

Hello

The gif above shows what this code will do. Scroll down for a breakdown of how it works.

xStartCoord := 66

run, mspaint.exe
sleep, 2000

Loop, 4
{
yStartCoord := 119
yEndCoord := 145

Loop, 4
{
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord, yEndCoord
MouseClickDrag, left, xStartCoord+20, yStartCoord, xStartCoord+20, yEndCoord
MouseClickDrag, left, xStartCoord, yStartCoord+10, xStartCoord+20, yStartCoord+10
sleep, 500
xStartCoord := xStartCoord+24
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord, yEndCoord
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord+8, yStartCoord
MouseClickDrag, left, xStartCoord, yStartCoord+10, xStartCoord+8, yStartCoord+10
MouseClickDrag, left, xStartCoord, yEndCoord, xStartCoord+8, yEndCoord
sleep, 500
xStartCoord := xStartCoord+15
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord, yEndCoord
MouseClickDrag, left, xStartCoord, yEndCoord, xStartCoord+9, yEndCoord
sleep, 500
xStartCoord := xStartCoord+15
MouseClickDrag, left, xStartCoord , yStartCoord, xStartCoord, yEndCoord
MouseClickDrag, left, xStartCoord, yEndCoord, xStartCoord+9, yEndCoord
xStartCoord := xStartCoord+15
MouseClickDrag, left, xStartCoord , yStartCoord, xStartCoord, yEndCoord
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord+10, yStartCoord
MouseClickDrag, left, xStartCoord+10, yStartCoord, xStartCoord+10, yEndCoord
MouseClickDrag, left, xStartCoord, yEndCoord, xStartCoord+10, yEndCoord

xStartCoord := xStartCoord-15-15-15-24
yStartCoord += 40
yEndCoord += 40
}
xStartCoord := xStartCoord+100
}
return

X and Y Coordinates in AutoHotkey Speak

Think of the X-axis as side to side. Y-axis is up and down. The confusing part is realizing that the top of the screen is 0 on the Y-axis and numbers increase as you get further down the screen.

X-Y

So to have the mouse automatically make an H with AutoHotkey, you’d need two parallel lines. The first two numbers after MouseClickDrag should be the starting point, the last two are the end.

MouseClickDrag1

For a single horizontal line, the Y-axis will not change. The X-axis coordinates should line up with those used above.

MouseClickDrag2

The beauty of coordinates in MouseClickDrag is that the X and Y’s can be variables. They can also be mathematical equations, meaning each point can be expressed in relation to one starting X and Y. So the above line is equivalent to the below.

xStartCoord := 66
yStartCoord :=129
MouseClickDrag, left, xStartCoord, yStartCoord, xStartCoord+20, yStartCoord

The benefit is that changing the xStartCoord changes every line that uses it. So if you want to run your code through a loop that writes HELLO several times on one line, you can just add

xStartCoord := xStartCoord+100

at the end of the loop. Every time the loop runs, your mouse will start 100 pixels to the right. Or, if you’d prefer several columns of the same MouseClickDrags, you could run it through 2 loops for adjustments on both the x and y axis. The first block of code on this page does exactly that with each loop running 4 times for a grand total of 16 HELLOs.

Advertisements
Tagged , , , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: