为何我的AutoHotkey v1脚本仅打开记事本,无法执行按键操作?
问题原因
脚本执行速度远快于记事本的启动加载速度,Run, notepad执行后,脚本立刻就运行了后续的Send命令,但此时记事本窗口还没完全打开、也没获得输入焦点,导致输入内容和快捷键操作都没有被记事本接收。
解决办法
最可靠的方式是等待记事本窗口激活后再执行输入操作,而非依赖固定延迟(固定延迟在不同设备上可能因加载速度差异失效)。修改后的代码示例如下:
Run, notepad WinWaitActive, ahk_exe notepad.exe ; 等待记事本窗口激活,此写法不受系统语言影响 Send hello world Send ^s
如果你的系统是中文环境,也可以直接用窗口标题匹配:
Run, notepad WinWaitActive, 无标题 - 记事本 Send hello world Send ^s
也可以先等待窗口存在再激活,效果一致:
Run, notepad WinWait, ahk_exe notepad.exe WinActivate, ahk_exe notepad.exe Send hello world Send ^s
内容的提问来源于stack exchange,提问作者Tage Lundgren
相关产品推荐
相关产品推荐

