为什么有时不分配变量?

Why variable is not assigned sometimes?

我有以下脚本:

Capslock::Backspace
Var := 123
#Z::MsgBox % "Var = '" . Var . "'"

当我按下 Win-Z 时,它显示:Var = ''。如果我删除第一行或将其移至文件末尾,一切都会按预期进行。这是什么原因?

赋值 Var := 123 需要驻留在 "Auto-execute Section" 中才能执行。具体来说,它需要在任何热键定义之前。

来自Autohotkey Documentation > Scripts

After the script has been loaded, it begins executing at the top line, continuing until a Return, Exit, hotkey/hotstring label, or the physical end of the script is encountered (whichever comes first). This top portion of the script is referred to as the auto-execute section.