如何在 AutoHotkey 中的热键内声明局部变量?

How do I declare a local variable within a hotkey in AutoHotkey?

TL:DR;我不能将热键内的变量声明为本地变量,这意味着 tempindex 是全局可访问的。

我最近发现local variables cannot be declared as local from within a hotkey or if they are used as parameters within a for-loop

^j::
   local temp = Hello, world!  ; Error: This line does not contain a recognized action
Return

SampleFunction() {
   for local temp in myArray {  ; Error: Variable name contains an illegal character
      ; do stuff
   }
}

这成为启用 #warn 的问题。除非我记得为我的每个 for 循环使用唯一的变量名称,否则我 运行 会出现以下错误:

Warning: This local variable has same name as a global variable. (Specifically: index)

例如:

#warn

^j::
   index = 42  ; This *index* variable is global
Return

UnrelatedFunction() {
   for index in myArray {  ; This *index* variable is local
      MsgBox % myArray[index] 
   }
}

尤其是在使用导入时,这会成为一个问题,因为我自己的脚本中的变量经常与我导入的脚本中的变量冲突。

理想情况下,我可以在任何 for 循环之前放置局部声明,但如前所述,我无法在热键中执行此操作。

是否可以在热键中将变量声明为本地变量?

我用函数实现了我的热键。默认情况下,函数内的变量具有局部作用域,除非它们被声明为 global

F1::alpha(10,10)
F2::alpha(20,30)
F3::beta()


alpha(x,y)
{
   myvar := x*2 + y
}

beta()
{
   myvar := 47
}