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

如何在Lua脚本启动后终止运行?添加按键终止功能问询

实现Lua脚本的中途终止功能

当然可以给脚本添加中途终止功能,核心思路是在脚本执行的关键节点(尤其是等待操作前后)检查终止按键状态,一旦检测到按键触发,立即停止后续操作。

实现方案

这里提供两种终止触发方式供你选择:

  • 再次按下原触发按键(即G_PRESSED且arg==4+GetMKeyState("kb")==3)
  • 指定独立终止按键(比如Esc键,操作更直观)

下面是修改后的完整脚本,同时包含两种检测逻辑,你可以根据需求调整:

-- 封装终止检查函数,集中处理检测逻辑
local function checkTerminate()
    -- 检测原触发按键再次按下
    if event == "G_PRESSED" and arg == 4 and GetMKeyState("kb") == 3 then
        return true
    end
    -- 检测Esc按键按下(可替换为你需要的终止键)
    if IsKeyPressed("esc") then
        return true
    end
    return false
end

if (event == "G_PRESSED" and arg == 4 and GetMKeyState("kb") == 3) then 
    MoveMouseTo( 36165, 57821)
    if checkTerminate() then return end
    Sleep(math.random(10, 25))
    if checkTerminate() then return end
    PressMouseButton(3)      
    ReleaseMouseButton(3)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    MoveMouseTo( 36131, 59461)
    if checkTerminate() then return end
    Sleep(math.random(30, 65))
    if checkTerminate() then return end
    PressKey ("lalt")
    if checkTerminate() then return end
    Sleep(math.random(30, 65))
    if checkTerminate() then return end
    PressKey ("X")
    if checkTerminate() then return end
    Sleep(math.random(30, 65))
    if checkTerminate() then return end
    ReleaseKey ("lalt")
    ReleaseKey ("X")
    if checkTerminate() then return end
    PressMouseButton(1)
    ReleaseMouseButton(1)
    if checkTerminate() then return end
    Sleep(math.random(2220, 2270))
    if checkTerminate() then return end
    MoveMouseTo( 38932, 56060)
    if checkTerminate() then return end
    Sleep(math.random(10, 25))
    if checkTerminate() then return end
    PressMouseButton(1)
    ReleaseMouseButton(1)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    MoveMouseTo( 2595, 3037)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressMouseButton(1)
    ReleaseMouseButton(1)
    if checkTerminate() then return end
    MoveMouseTo( 4166, 1518)
    if checkTerminate() then return end
    Sleep(math.random(250, 350))
    if checkTerminate() then return end
    PressMouseButton(1)
    ReleaseMouseButton(1)
    if checkTerminate() then return end
    MoveMouseTo( 38932, 56060)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressMouseButton(3)
    ReleaseMouseButton(3)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    MoveMouseTo( 38590, 57882)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressKey ("lalt")
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressKey ("X")
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    ReleaseKey ("lalt")
    ReleaseKey ("X")
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressKey ("O")
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    ReleaseKey ("O")
    if checkTerminate() then return end
    Sleep(200)
    if checkTerminate() then return end
    MoveMouseTo( 54436, 55453)
    if checkTerminate() then return end
    Sleep(math.random(40, 135))
    if checkTerminate() then return end
    PressMouseButton(1)
    ReleaseMouseButton(1)
    if checkTerminate() then return end
    Sleep(400)
    if checkTerminate() then return end
    MoveMouseTo( 54299, 48650)
    if checkTerminate() then return end
    Sleep(150)
    if checkTerminate() then return end
    PressMouseButton(1)      
    ReleaseMouseButton(1)
end

关键说明

  1. checkTerminate()函数集中处理终止检测逻辑,后续修改终止键只需改动这个函数即可
  2. 在每个操作(鼠标移动、按键、等待)后加入检查,一旦触发终止条件,用return直接终止整个脚本块执行
  3. 若不需要两种终止方式,删除checkTerminate()中对应检测代码即可
  4. 注意:如果你的脚本运行环境中按键检测函数不是IsKeyPressed,需要替换为对应环境的函数名

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 07:05:24