The documentation on global variables can be found here:
https://autohotkey.com/docs/Functions.htm#Global
Global variables
To refer to an existing global variable inside a function (or create a new one), declare the variable as global before using it. For example:
LogToFile(TextToLog) { global LogFileName FileAppend, %TextToLog%`n, %LogFileName% }
I believe that the concept of the global, with AHK, is slightly different from the concept in other languages. With AHK, you can create a variable and use it in several hot keys and routines without declaring them global.
Gv := 0 f1::SetTimer, Action, % (on:=!on) ? (1000) : ("Off") Action: Gv++ trayTip,, % Gv Return f2::Msgbox, % Gv
Code Explanation:
- The F1 key toggles the timer to start the routine:
Action
every 1000
ms. %
starts the expression.on:=!on
changes the binary value of the on
variable each time F1 is pressed.?:
together called the ternary operator.- When the value on = 1 is set to
1000
ms; when enabled = 0, the timer turns Off
.
The ++
operator adds 1 to the Gv variable.
source share