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

Logitech Lua宏按键绑定求助:鼠标4/5触发不同宏失效

修复后的Logitech Lua宏代码

原代码核心问题是侧键5的判断逻辑错误嵌套在侧键4的条件块内,导致侧键5永远无法触发,以下是修复并优化后的完整代码:

do
    local function busyloop(final_ctr)
       final_ctr = final_ctr - final_ctr%1
       local ctr, prev_ms, ms0, ctr0 = 0
       while ctr ~= final_ctr do
          local ms = GetRunningTime()
          if prev_ms and ms ~= prev_ms then
             if not ms0 then
                ms0, ctr0 = ms, ctr
             elseif final_ctr < 0 and ms - ms0 > 500 then
                return (ctr - ctr0) / (ms - ms0)
             end
          end
          prev_ms = ms
          ctr = ctr + 1
       end
    end
    local coefficient = busyloop(-1)
    function FastSleep(ms)
       return busyloop(ms * coefficient)
    end
end

function OnEvent(event, arg)
    -- 仅处理鼠标按键按下事件
    if event ~= "MOUSE_BUTTON_PRESSED" then return end

    -- 侧键4触发的宏
    if arg == 4 then
        repeat
            PressMouseButton(1)
            MoveMouseRelative(-1, 1)
            FastSleep(1)
            ReleaseMouseButton(1)
            -- 微小延迟避免过度占用资源
            FastSleep(1)
        until not IsMouseButtonPressed(4)
    end

    -- 侧键5触发的宏
    if arg == 5 then
        repeat
            PressMouseButton(2)
            MoveMouseRelative(-2, 2)
            FastSleep(2)
            ReleaseMouseButton(2)
            FastSleep(1)
        until not IsMouseButtonPressed(5)
    end
end

关键修复与优化点

  • 修正代码结构:将侧键5的逻辑从侧键4的条件块中移出,确保两个按键的宏各自独立触发。
  • 过滤无效事件:提前判断事件类型,非按键按下事件直接返回,减少冗余逻辑。
  • 增加稳定性延迟:在循环操作后添加短延迟,避免宏运行时过度占用驱动资源。
  • 保留速度优化:保留你原本用于解决Windows更新后宏变慢的FastSleep函数,确保速度优化效果。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 03:55:15