0
votes
; Give user the opportunity to choose his own hotkey
Gui, Add, Hotkey, x21 y234 w240 h30 vPanicKey gRunPanicKey,^F12

global myList := ["foo"]

RunPanicKey:
    if(PanicKey != "") { ; Make sure the hotkey chosen by the user isn't empty.
        myList.Insert("bar") ; Insert new string into the array.
        myList2 := myList[2] ; Get 2nd index value and store it in myList2
        MsgBox,0,My Array, The 2nd value of myList is: %myList2%
    }
return

It appears that myList.Insert() does not work here, because the script cannot find the array, therefore myList2 is empty. But how come? I thought I made the array global?

1
afaik, in AHK you dont need to declare a variable as global except within a function - phil294
also, I dont understand your problem. I ran your program (added gui show). every time I enter a key, a msgbox correctly says the 2nd value is bar, because that's what's being assigned - phil294
I'm using AutoHotkey 1.1.22.02 and it doesn't work for me. It just says The 2nd value of myList is: .. - Dean
you should post the complete source code then - phil294

1 Answers

0
votes
GuiClose:
    ExitApp
return

global myList := ["foo"]

As seen from your comment, the problem lies not within the global keyword (which you actually do not need at all here). The reason your program is not working is that return statement. Everything after the first return in a script does not get executed automatically on script start. See https://autohotkey.com/docs/Scripts.htm#auto for details. So, simply move the variable initialization at the very beginning of your file.