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

请求合并两个GHUB Lua脚本:后坐力控制与鼠标绑定

GHUB Lua脚本合并延迟问题解决

问题描述

我是Lua编程新手,用GHUB时遇到个问题——没法同时加载两个Lua脚本,得把它们合并:

  • 第一个是简易后坐力脚本:按住鼠标3时,按鼠标1会让鼠标相对下移
  • 第二个是带ScrollLock开关的绑定脚本:开启ScrollLock后,按鼠标1会触发按下并释放LAlt/鼠标3
    自己合并后出现了延迟问题,求帮忙。

原脚本

后坐力脚本

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
if IsMouseButtonPressed(3)then
repeat
if IsMouseButtonPressed(1) then
repeat
MoveMouseRelative(0,2)
Sleep(11.0)
until not IsMouseButtonPressed(1)
end
until not IsMouseButtonPressed(3)
end
end

绑定脚本

function OnEvent(event, arg)
if IsKeyLockOn("scrolllock")then ------ (我想要能开关这个功能)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
        repeat
            PressKey("lalt")   ------- (或者换成鼠标3)
            Sleep(20)
            ReleaseKey("lalt") ------- (或者换成鼠标3)
        until not IsMouseButtonPressed(1)
    end
end

问题原因

你之前合并出延迟的核心问题有两个:

  1. GHUB只会执行最后定义的OnEvent函数,之前的会被直接覆盖,等于其中一个脚本逻辑根本没生效,或者强行嵌套循环导致阻塞;
  2. 原脚本里用了多层repeat-until嵌套,这种结构会完全阻塞GHUB的事件循环,导致其他输入响应延迟。

合并后的无延迟脚本

EnablePrimaryMouseButtonEvents(true)

-- 用变量跟踪功能状态,避免重复检测
local is_recoil_mode = false
local is_bind_enabled = IsKeyLockOn("scrolllock")

function OnEvent(event, arg)
    -- 监听ScrollLock开关状态变化
    if event == "KEYBOARD_LOCK_TOGGLED" and arg == "scrolllock" then
        is_bind_enabled = not is_bind_enabled
    end

    -- 后坐力模式开关:按住鼠标3激活,松开关闭
    if event == "MOUSE_BUTTON_PRESSED" and arg == 3 then
        is_recoil_mode = true
        -- 循环检测直到鼠标3松开
        while is_recoil_mode do
            -- 如果同时按住鼠标1,执行后坐力下移
            if IsMouseButtonPressed(1) then
                MoveMouseRelative(0, 2)
            end
            Sleep(11)
            -- 更新状态,避免死循环
            is_recoil_mode = IsMouseButtonPressed(3)
        end
    elseif event == "MOUSE_BUTTON_RELEASED" and arg == 3 then
        is_recoil_mode = false
    end

    -- 绑定触发逻辑:ScrollLock开启时生效
    if is_bind_enabled and event == "MOUSE_BUTTON_PRESSED" and arg == 1 then
        -- 循环直到鼠标1松开
        while IsMouseButtonPressed(1) do
            PressKey("lalt")
            Sleep(20)
            ReleaseKey("lalt")
            -- 加个小延迟,避免触发过于频繁同时不阻塞事件
            Sleep(10)
        end
    end
end

关键优化点

  • 把两个OnEvent合并成一个,确保所有逻辑都能被执行;
  • 用变量跟踪功能状态(is_recoil_mode、is_bind_enabled),减少重复的系统调用;
  • 调整循环结构,避免嵌套阻塞,每个循环里的Sleep不会长时间占用事件线程;
  • 监听KEYBOARD_LOCK_TOGGLED事件实时更新ScrollLock状态,不用每次操作都检测锁状态。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.25 21:05:35