TSolo315 MturkCrowd
Six years of turking experience

AutoHotkey Basics

Learning the fundamentals.

1

Setting Hotkeys.


As you learned in the previous tutorial, hotkeys are set using the following format: hotkey:: . You also learned that the ^ character stood for the control button. Here are some other common characters used in AHK:

  • The # character is used for the Win (windows logo) key.
  • The ! character is used for the Alt key.
  • The + character is used for the Shift key.

So !+z:: would stand for Alt-Shift-Z.

2

Sending Text and Keystrokes.


You have learned that text is sent using the sendinput command. You can also use the special characters above to send key combinations. For example sendinput ^t would send the key combination Control-T. If you want to send a keypress that is not a common letter or number, you have to surround that key in curly brackets. For example sendinput {tab} represents a tab key press. Common keys used this way are {tab}, {enter}, {space}, {ctrl}, {up}, {down}, {left}, {right}. You can also individually set key down and key up commands, allowing you to press several other keys in between a key being pressed down and the key being released. For example:


^t::
sendinput {ctrl down}tr55{ctrl up}
return

3

Adding a Pause.


Scripts made in AutoHotkey run very fast. They often run too fast. If you have a script that opens a new tab in your web browser and then directs you to a new webpage -- a pause is required to allow for the browser to load the webpage before you can issue any commands interacting with it. Without a pause, the script will send commands while the page is loading and it won't work correctly. You can add a pause to your script by using the sleep command. sleep 2000 would pause your script for two seconds (time is set in milliseconds.)

4

Interacting with the Clipboard.


Using the clipboard (where information is saved to when you use copy in windows) is often a necessity when writing AHK scripts for Mechanical Turk HITs. Luckily, AHK makes it easy with a built in variable conveniently named "clipboard." This variable holds the current contents of your clipboard and can be used to retrieve those contents or to change them. This becomes particularly useful when working with custom variables, which we will discuss in a later tutorial. Here are some ways you can interact with the clipboard:


clipboard := "my text"   ; Give the clipboard entirely new contents.
clipboard := ""   ; Empty the clipboard.
clipboard := clipboard " Mturk AHK Script"   ; Append some text to the clipboard.
StringReplace, clipboard, clipboard, ABC, DEF, All   ; Replace all occurrences of ABC with DEF (also converts the clipboard to plain text).

We will go over methods involving the clipboard in more detail in later tutorials.

5

Adding a Comment


Adding comments to your scripts can help you remember what exactly certain code snippets are meant to do. While you may think you have a good memory and can keep it all in your head, it's not uncommon for even experienced programmers to come back to code they had written months ago and not remember exactly what does what. Adding comments is easy: all you have to do is add a semicolon ; and anything on the line afterward will be considered a comment and will not affect your code. Comments can be seen in the clipboard examples above detailing what each line of code will do.

6

Working with default Windows/Browser Hotkeys


Taking advantage of built in windows or browser hotkeys can save you a lot of time and effort. For example, in a browser, sending the hotkey Control-C will copy whatever is currently highlighted, Control-V will then paste that copied information. Control-T will open a new empty browser tab, Control-L will place your cursor in your browsers address bar, Control-Tab will switch to the next tab, Control-Shift-Tab will switch to the previous tab. Here are a few hotkey overviews:

7

Review


Here is a script that utilizes everything we have learned in this tutorial:


^8::
sendinput ^t ; send a control-t keypress to open a new browser tab
sleep 1000 ; sleep for a second to give the tab time to open
sendinput https://worker.mturk.com ; input the mturk url in your address bar
sendinput {enter} ; send an enter keypress
sleep 4000 ; sleep for four seconds to give the page time to load
sendinput ^a ; send control-a to select all text on screen
sendinput ^c ; send control-c to copy all the text
StringReplace, clipboard, clipboard, survey, bubbles, All ; replace all instances of the word survey with the word bubbles (in your clipboard)
sleep 1000 ; sleep for a second
sendinput ^l ; send the hotkey control-l to focus your cursor in the address bar
sendinput https://www.rapidtables.com/tools/notepad.html ; input the link to a notepad website
sendinput {enter} ; send an enter keypress
sleep 3000 ; sleep for three seconds to give the page time to load
sendinput ^v ; a control-v keypress to paste
return

Perhaps not very practical but at this point you should start to see the potential for writing scripts to make HITs more efficient.

Next Tutorial: Hotstrings