You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为Lua自动点击脚本添加总休眠时长打印功能求助

解决方案

这个需求完全可以实现,你之前触发“attempt to call a nil value”报错,大概率是误用了Lua标准库的print函数——在这类鼠标宏脚本环境(比如罗技G HUB)中,控制台输出要用OutputLogMessage函数,而非print。

修改后的脚本如下:

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event, arg)
    if IsKeyLockOn("capslock") then
        if IsMouseButtonPressed(5) then
            repeat
                -- 生成两次休眠的随机值并存储
                local sleep1 = math.random(51, 55)
                local sleep2 = math.random(526, 530)
                -- 计算总休眠时长并打印到控制台
                local total_sleep = sleep1 + sleep2
                OutputLogMessage("本次循环总休眠时长:%d\n", total_sleep)
                
                PressMouseButton(1)
                Sleep(sleep1)
                ReleaseMouseButton(1)
                Sleep(sleep2)
            until not IsMouseButtonPressed(5)
        end
    end
end

关键改动说明

  • 把两次Sleep的随机值提前用变量存储,避免重复生成不同的随机数
  • 使用OutputLogMessage输出总休眠时长,该函数是这类宏脚本环境的标准输出接口,支持格式化字符串
  • 每次循环开始前就计算并打印总时长,完全匹配你的需求

内容的提问来源于stack exchange,提问作者BobsStackOverflow

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.26 00:18:22