AutoHotKey - 声明全局数组不起作用
AutoHotKey - Declaring global array does not work
; 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
看来myList.Insert()
在这里不起作用,因为脚本找不到数组,所以myList2
是空的。但是怎么会呢?我以为我把数组设为全局了?
GuiClose:
ExitApp
return
global myList := ["foo"]
从您的评论中可以看出,问题不在于 global
关键字(实际上您在这里根本不需要)。您的程序无法运行的原因是 return
语句。 脚本中第一个 return 之后的所有内容 不会 在脚本启动时自动执行。 有关详细信息,请参阅 https://autohotkey.com/docs/Scripts.htm#auto。因此,只需将变量初始化移动到文件的开头即可。
; 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
看来myList.Insert()
在这里不起作用,因为脚本找不到数组,所以myList2
是空的。但是怎么会呢?我以为我把数组设为全局了?
GuiClose:
ExitApp
return
global myList := ["foo"]
从您的评论中可以看出,问题不在于 global
关键字(实际上您在这里根本不需要)。您的程序无法运行的原因是 return
语句。 脚本中第一个 return 之后的所有内容 不会 在脚本启动时自动执行。 有关详细信息,请参阅 https://autohotkey.com/docs/Scripts.htm#auto。因此,只需将变量初始化移动到文件的开头即可。