AHKv2调用不存在函数错误:InputBox倒计时脚本排查求助
问题:AutoHotkey脚本出现“Call to nonexistent function”错误排查
我尝试创建一个简单的InputBox弹窗,用于输入小时:分钟格式的时间,随后启动倒计时器。当计时器归零时,激活指定窗口并向该窗口发送输入内容。我已参考官方文档编写脚本,但反复出现Call to nonexistent function错误。为排查问题,我不断简化脚本,现在几乎和文档示例一致,却仍报错,恳请帮忙排查。
错误脚本如下:
:*:obstimer:: ;; Show the input box USER_INPUT := InputBox("This is the prompt","This is the title",W200 H300 T30,"").value ;; Split the input time into hours and minutes TimeArray := StrSplit(USER_INPUT, ":") hours := TimeArray[1] ; Get the hours part minutes := TimeArray[2] ; Get the minutes part ;; Convert the hours and minutes to seconds loop_timer = (hours * 3600) + (minutes * 60) ;; Set a timer that will trigger the press_backtick function every 1000 milliseconds (1 second) SetTimer, press_backtick, 1000 return press_backtick: loop_timer-- ; Decrement the loop timer by 1 if (loop_timer <= 0) { ; If the loop timer is less than or equal to 0 WinActivate, ahk_exe obs64.exe ; Activate the obs64.exe window ControlSend, , {U+0060}, ahk_exe obs64.exe ; Send the backtick character ` to the obs64.exe window SetTimer, press_backtick, off ; Turn off the timer } return
错误原因
核心问题是混用了AutoHotkey v1和v2的语法:
- 你参考的是AHK v2的InputBox文档,但脚本整体是v1风格的写法(比如
SetTimer, press_backtick, 1000这种逗号分隔参数、用=赋值) - v1中InputBox是命令而非函数,你用v2的函数调用格式
InputBox(...),导致v1环境无法识别,直接触发“调用不存在的函数”错误。
修正后的v1版本脚本
:*:obstimer:: ;; 显示输入框(v1 InputBox命令格式) InputBox, USER_INPUT, This is the title, This is the prompt,, 200, 300,,,,, 30 ;; 处理用户取消输入的情况 if ErrorLevel return ;; 拆分时间为小时和分钟 TimeArray := StrSplit(USER_INPUT, ":") hours := TimeArray[1] minutes := TimeArray[2] ;; 转换为总秒数(用:=做表达式赋值,避免字符串赋值错误) loop_timer := (hours * 3600) + (minutes * 60) ;; 设置1秒间隔的计时器 SetTimer, press_backtick, 1000 return press_backtick: loop_timer-- if (loop_timer <= 0) { WinActivate, ahk_exe obs64.exe ControlSend, , {U+0060}, ahk_exe obs64.exe SetTimer, press_backtick, off } return
关键点说明
- InputBox用法:v1中InputBox是命令,格式为
InputBox, 输出变量名, 标题, 提示文本,, 宽度, 高度,,,,, 超时时间 - 错误处理:加入
ErrorLevel判断,用户点击取消时直接退出热键 - 赋值方式:用
:=代替=做表达式赋值,确保时间换算的数值计算正确
若改用AutoHotkey v2版本,脚本如下
:*:obstimer:: { ;; 显示输入框(v2函数式调用) inputResult := InputBox("This is the prompt", "This is the title", "W200 H300 T30") if (inputResult.Result != "OK") return USER_INPUT := inputResult.Value ;; 拆分时间 TimeArray := StrSplit(USER_INPUT, ":") hours := TimeArray[1] minutes := TimeArray[2] ;; 转换为总秒数 loop_timer := (hours * 3600) + (minutes * 60) ;; 设置计时器 SetTimer(press_backtick, 1000) press_backtick() { global loop_timer loop_timer-- if (loop_timer <= 0) { WinActivate("ahk_exe obs64.exe") ControlSend("ahk_exe obs64.exe", "{U+0060}") SetTimer(A_ThisFunc, 0) ; 关闭计时器 } } }
内容的提问来源于stack exchange,提问作者misteralexander
相关产品推荐
相关产品推荐

