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

标签 scope autohotkey local-variables

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

我最近发现 local variables不能从热键中声明为本地,或者如果它们被用作 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
}

关于scope - 如何在 AutoHotkey 的热键中声明局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45967741/

相关文章:

C++函数返回静态数组地址

java - 了解 web 应用程序中的 spring bean 作用域

javascript - js : accessing scope of parent class

oracle - 封装规范中的程序

autohotkey - 如何在 AutoHotkey 中暂时禁用鼠标左键激活的代码?

c - 局部变量排序问题

ruby - 为什么我不能在方法外定义变量并在方法内更新?

intellij-idea - 如何从终端运行自定义 IntelliJ 检查

delphi - WM_Copy、wm_gettext 和 wm_keydown 失败?

keyboard-shortcuts - 如何将拦截的 key 传递给 autohotkey 中的应用程序