rayden54
rayden54

Reputation: 251

Could someone explain this autohotkey script to me?

It's supposed to let me turn a toggle a run button on and off in a game (instead of having to hold it down). It works, but I'd like to know how.

$w:: Gosub, AutorunToggleME2
~s:: Gosub, AutoRunOffME2

AutorunToggleME2:
toggle := !toggle
Send % "{w " . ((Toggle) ? ("down") : ("up")) . "}"
return

AutoRunOffME1:
toggle = ; Off
Send {w up}
return

Specifically, I'd like what the following lines do:

Send % "{w " . ((Toggle) ? ("down") : ("up")) . "}"

Send {w up}

Upvotes: 1

Views: 116

Answers (1)

Blorgbeard
Blorgbeard

Reputation: 103525

The code is pretending to hold down w. The basic Send syntax is explained here.

To hold down or release a key: Enclose in braces the name of the key followed by the word Down or Up. For example:

Send {b down}{b up} 
Send {TAB down}{TAB up} 
Send {Up down}  ; Press down the up-arrow key. 
Sleep 1000  ; Keep it down for one second. 
Send {Up up}  ; Release the up-arrow key.

So this line:

Send {w up}

Clearly is releasing w.

The other line is more complicated:

Send % "{w " . ((Toggle) ? ("down") : ("up")) . "}"

You can find explanations of the relevant syntax here.

Basically:

  • % says that the following text is an expression.
  • The . is a string-concatenation operator.
  • This part ((Toggle) ? ("down") : ("up")) is the conditional operator (a.k.a ternary operator). It's shorthand for an if/else statement. In this case, when Toggle is true, it returns "down", otherwise it returns "up".

This works out to either Send {w down} or Send {w up} depending on the value of Toggle (true or false)

Upvotes: 4

Related Questions