Friday, December 13, 2013

AutoHotKey examples

AutoHotKey is a very useful scripting tool for creating macros. Here's some example code snippets for you!

Message box:

MsgBox Hello, world!

Message box with cancel. Pressing the Cancel button will stop the current script.

MsgBox, 1,, Do you really want to start this operation?
IfMsgBox, Cancel

Setting a variable. This will wait for 2 seconds.

DelayMilliseconds = 2000
Sleep DelayMilliseconds

Sending keypresses to the currently active window. Explanations as comments after the semicolon.

Send {Enter}
SendRaw Hello ;Sends the text "Hello"
Send {LAlt} ;Left alt
Send {Del}
Send ^a ;Ctrl+a
Send !a ;Alt+a
Send {Home}
Send {F10}

Searching for and activating windows that begin with a certain string:

SetTitleMatchMode 1 ;search windows beginning with the string
IfWinExist Internet Explorer
    WinActivate ;This will activate the window that was last searched for

    ;Add code here...

Checking if a window is NOT active:

If !WinActive("Internet Explorer")
       MsgBox Error, window not active

Responding to keypress F12:

MsgBox you pressed F12!

Responding to keypress Win+a:

MsgBox you pressed Win+a!