我知道我忽略了一些显而易见的东西,但我不明白为什么这不起作用。为什么 hello 没有出现在第一个消息框中?我知道如果我取消注释 #Warn,它会说变量未分配,但这是正确的吗?这是 ahk 文件中唯一的东西。
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
#SingleInstance force
; Reload the script
^!z::
WinActivate, ahk_class Notepad++
Send {ctrl down}s{ctrl up}
sleep 100
Reload
return
ADPass = hello
!5::
MsgBox, %ADPass%
Msgbox, test
return
答案1
或者:在脚本读取行直到遇到任何命令(如:Return、Exit 或按键绑定(key/s::somecode))时对变量进行赋值,换句话说 - 自动执行部分,或者在您的按键内进行赋值:
...
SetWorkingDir, % A_ScriptDir
ADPass:="hello"
; reload the script
^!z::
WinActivate, % "ahk_class Notepad++"
Send, {CtrlDown}{s}{CtrlUp}
Sleep, 100
Reload
Return
!5::
; ADPass:="hello"
MsgBox, % ADPass
Msgbox, % "test"
Return