Hi,
in this tutorial I will show you how to use AutoIt to automate tasks that include moving and clicking your mouse and pressing keys. Why would you want to use AutoIt? Because it is free and very powerful, when you learn it properly.
1. Intro
AutoIt is in fact a scripting language with very simple syntax. AutoIt allows you to use Windows GUI and many more Windows functions from the script. You can for example minimize a window, play sound, type some text, move mouse cursor to specific coordinates and perform a click.
Learning programming, that is a story for another time. You can imagine that programming is like telling the computer what it should do in short sentences in some language and there are many languages computers understand. The language define few keywords, mathematical operators and few control flow constructs (loops and branching). You can imagine this as a program:
Code:
go to shop
if they have eggs then
x = eggs
else
x = pizza
end if
buy x
go home
cook x
while hungry
eat a bit of x
end while
AutoIt defines few functions (similar to buy x) that you need to understand, we will get to that.
2. Download and Install AutoIt
Go to AutoIt download page and download full installation of the latest version. It comes with a nice editor that does 'syntax highlighting'. Which means it will color the script for better readability.
Install everything. At the end, select the default option to RUN scripts.
3. First program
Programming tutorials usually begin with Hello World program that just prints Hello World. I will explain on this program some functions of AutoIt you will be very probably calling in each script.
Open SciTE editor. It's like a better notepad. Select File->New and create a new file. Copy the following to the editor. The code is explained in comments. Comments are the lines starting with semicolon. These lines are not interpreted by AutoIt. They are just text notes. Programmers use comments to explain what is the code doing. Save it to any directory where you can find it (Documents is fine) under some name, like "Hello.au3"
Code:
; this will bring the notepad windows to the foreground and activates
WinActivate("Untitled - Notepad")
WinWaitActive("Untitled - Notepad")
; This will type "Hello World" in notepad
Send("Hello World")
First open Notepad. Make sure the title of notepad says Untitled - Notepad. If it says something else (e.g. you have localized Windows) change the "Untitled - Notepad" in the script to whatever your notepad's title is.
There are two functions that we use to activate this window:
WinActivate - this sends the command to the system to activate the window with the given title
WinWaitActive - this pauses our program until the window is really activated (use this two command always like this, becase there can be a delay between the command and the window going actually active)
To run the program, make sure you have notepad running. If you have this all set up, just run the script the same way you open any other file or executable. It will activate notepad window and type the text there...
4. Things you will be using the most
There are only a few functions you will be using and you need to understand:
MouseMove
This will move the mouse cursor to a position on the screen (x, y).
MouseClick
Code:
MouseClick("primary")
MouseClick("secondary")
MouseClick("middle")
This will obviously make a mouse click. Note: You can use "left" and "right" but if you have swapped buttons (left handed mouse), right = primary.
Send
Code:
Send("text")
Send("{F10}")
Send("+x")
Sends keystrokes to the system. System will decide where to actually send it (this will be your active window in 99% of cases). If you write the text in curly bracers, it actually means the name of the key (like {F10}, {ESC}). You can also send modifier keys like Shift, Alt, Ctrl, if you prefix the key with +, ! or ^. See the documentation of this command for further reference (table in the bottom).
ControlSend
Code:
ControlSend( "World of Warcraft", "", "", "{TAB}")
ControlSend( "World of Warcraft", "", "", "{ENTER}Hello World{ENTER}")
This sends keystrokes to the windows you specify. In our case, the window named "World of Warcraft". It will send the keys even to a minimized window. So you can actually let WoW run on background and still send keys there.
Sleep
This will pause the execution of the script for the given period in ms. E.g. 1000ms = 1 second.
You need to understand three more concepts
Variables
Variable is like a memory bank that stores a value. The value can be anything. Variables in AutoIt start with $ followed by name. The character '=' means the value on the right is assigned to the variable on the left. Examples:
Code:
$x = "Hello World" ; this is a string
$y = 10 ; this is an integer
$z = $y + 5
$s = $x & y
Wherever you can use a value, you can use a variable as well. You can assign a value to variable and perform operations on it. E.g. if you would have printed the value of $z from the last code snippet, it would print '15'. You can also perform operations on strings, again, in the snippet, value of $s would be 'Hello World10'.
Loops
There will be two loops you will be using. Finite and infinite. Programmers usually work with conditions and stuff, but let's leave this for now.
Infinite Loop, it will execute whatever commands you have put inside the loop until the program is terminated (like Alt+F4, I will show you how to do that)
Code:
While True
; your code here
WEnd
Finite Loop, when you need to perform something x-times.
Code:
For $i = 1 to 10 Step 1
; your code here
Send("Iteration no: " & $i & "{ENTER}")
Next
This loop will be executed 10 times (from 1 to 10 by a step +1). Inside the loop you can read the value of variable $i - it contains the number of the current iteration. If you change the Hello World program to:
Code:
; this will bring the notepad windows to the foreground and activates
WinActivate("Untitled - Notepad")
WinWaitActive("Untitled - Notepad")
For $i = 1 to 10 Step 1
; your code here
Send("Iteration no: " & $i & "{ENTER}")
Next
It will insert the following to Notepad:
Code:
Iteration no: 1
Iteration no: 2
Iteration no: 3
Iteration no: 4
Iteration no: 5
Iteration no: 6
Iteration no: 7
Iteration no: 8
Iteration no: 9
Iteration no: 10
Functions
The same way there exist function MouseClick, we can make our own function. Function is nothing else than a piece of code that can take arguments and return a value. Let's not complicate this for now and let me show you one very useful function that we will use.
Code:
Func MyExit()
Exit
EndFunc
This function takes no arguments and it calls system function Exit, which will end our program. Why do we need to put this into function?
Code:
Func MyExit()
Exit
EndFunc
HotKeySet("^!x", "MyExit")
AutoIt defienes a function HotKeySet that takes two arguments, the hot key (in our case CTRL+ALT+x) and the name of another function that will be called after you press the hotkey (in our case our function MyExit).
Now you understand some functions of AutoIt. If you find it easy enough, google for a real tutorial and learn how to script. AutoIt is a great tool that can do even more stuff like reading memory. With very little knowledge of programming, you could be actually able to create your own bot, that will be almost undetectable by Warden.
Let's write a program, that will do something useful.
5. Putting it all together
Code:
; our function to exit the program
Func MyExit()
Exit
EndFunc
; set the CTRL+ALT+x hotkey to exit the program
HotKeySet("^!x", "MyExit")
; this will bring the WoW window to the foreground and activates
WinActivate("World of Warcraft")
WinWaitActive("World of Warcraft")
; infinite loop
While True
; press key '2'
Send("{2}")
; wait for one second
Sleep(1000)
WEnd
So what this does is that it activates WoW window and start spamming key '2' every second. When you press CTRL+ALT+x it stops. If you want to try it out, log in to the game, bind some macro/spammable skill to key 2. Or you can use any other key given you change the Send command in the script to spam whatever key you have used. Run the script.
Imagine the Deathbringer Saurfang fight as a melee. You could easily script the whole fight!
Now you can start playing with MouseMove, MouseClick or you can try changing the Send to ControlSend and browse the web while AutoIt spams something to minimized WoW.
---------- Post added at 10:57 PM ---------- Previous post was at 10:53 PM ----------
Bonus: Tool to get the mouse position coordinates
When you start playing with MouseMove you will need to know where exactly is the thing we want to click on. The x,y coordinates.
I could give you an executable to do this, or you can use whatever you have, but why don't we just write a script for it? This need a little bit more understanding of AutoIt, but you can figure out how it works from the comments. If you don't want to bother, just copy&paste it to any file (like cursor.au3) and run the script.
[SPOILER]
Code:
; include some constants we use, like $GUI_EVENT_CLOSE or $WS_EX_TOOLWINDOW
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
; Create a GUI window
; title will be 'Cursor nfo'
; width = 160, height = 40. The two -1 means the window will be centered on screen (this is AutoIt's doing,
; it's like we haven't specified WHERE we want the window exactly, so it just puts it in the middle)
; $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST - these are two constants - variables with predefined values, that
; tell the GUI to draw a windows with small border and always on top
GUICreate("Cursor nfo", 160, 40, -1, -1, -1, $WS_EX_TOOLWINDOW + $WS_EX_TOPMOST)
; Create two labels, don't worry about text being empty for now, the reset is left, right, width, height
$label1 = GUICtrlCreateLabel("", 5, 5, 150, 15)
$label2 = GUICtrlCreateLabel("", 5, 20, 150, 15)
; helper variables, storing last known cursor position and color
$ox = -1
$oy = -1
$oc = -1
; show our GUI window
GUISetState(@SW_SHOW)
; In Infinite Loop do
While True
; check GUI Messages (what windows sends to us) for possible events
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE ; in the event user click the close button
Exit
EndSwitch
; call function MousePos
MousePos()
; sleep for 25ms, this is here just so we don't read the cursor every processor tick
Sleep(25)
WEnd
; Function that reads the cursor
Func MousePos()
; get the mouse cursor position, absolute
$pos = MouseGetPos()
; get pixel color
$col = PixelGetColor($pos[0], $pos[1])
; if anything changed (i.e. any saved value is not the same as current value)
If ($ox <> $pos[0] OR $oy <> $pos[1] OR $oc <> $col) Then
; save the current value
$ox = $pos[0]
$oy = $pos[1]
$oc = $col
; update the labels with the current readings
SetLabels($pos[0], $pos[1], $col)
EndIf
EndFunc
; set label text
Func SetLabels($x, $y, $col)
; construct the text
$textPos = "Mouse pos x:" & $x & " y:" & $y
$textCol = "Color: " & Hex($col, 6)
; set the text to the labels
GUICtrlSetData($label1, $textPos)
GUICtrlSetData($label2, $textCol)
EndFunc
[/SPOILER]