Global variable does not have global scope

前端 未结 4 2039
予麋鹿
予麋鹿 2021-02-13 04:42
supposedlyGlobalVariable := \"blah\"

ARoutine()
{
   localVariable := \"asdf\"
   MsgBox, The global variable value is %supposedlyGlobalVariable%.  The local variable v         


        
4条回答
  •  攒了一身酷
    2021-02-13 05:17

    The documentation for 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 prior to using it. For example:

    LogToFile(TextToLog)
    {
        global LogFileName
        FileAppend, %TextToLog%`n, %LogFileName%
    }
    

    I believe the concept of global, with AHK, is a bit different than in other languages. With AHK you can create a variable and use it within multiple hotkeys, and subroutines, without declaring it as global.

    Gv := 0
    
    f1::SetTimer, Action, % (on:=!on) ? (1000) : ("Off")
    
    Action:
        Gv++
        trayTip,, % Gv
    Return
    
    f2::Msgbox, % Gv
    

    Explaination of code:

    • The F1 key toggles a timer to run the subroutine: Action every 1000ms.
    • % starts an expression.
    • on:=!on reverses the binary value of variable on every time F1 is pressed.
    • ?: together is called the ternary operator.
    • When on=1 delay is set to 1000ms; when on=0 the timer is turned Off.

    The ++ operator adds 1 to variable Gv.

提交回复
热议问题