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

Logitech G Hub Lua脚本求助:实现鼠标XY轴同步移动

问题:Logitech G Hub Lua脚本无法实现鼠标XY轴同时移动

我需要实现鼠标XY轴同时移动的功能,但当前脚本仅能控制X轴。原本以为复制X轴代码并修改为Y轴参数就能生效,但实际没有效果。

原代码

EnablePrimaryMouseButtonEvents(true)
function SmoothMouseMovement(x_speed, y_speed, duration)
    local last_x, last_y = 0, 0
    local tm_start = GetRunningTime()
    repeat
        Sleep(5)
        if not IsMouseButtonPressed(1) then break end
        local tm = math.min(GetRunningTime() - tm_start, duration)
        local x = math.floor(x_speed * tm)
        local y = math.floor(y_speed * tm)
        if x ~= last_x or y ~= last_y then
            MoveMouseRelative(x - last_x, y - last_y)
            last_x = x
            last_y = y
        end
    until tm >= duration
end
function OnEvent(event, arg)
    --OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
    if IsKeyLockOn("capslock") and IsMouseButtonPressed(1) then
        repeat
            local x_speed = 2  -- pixels per millisecond (amount of pixels mouse will move per ms)
            local duration = 30 -- milliseconds (amount of time mouse will move in one direction for)
            SmoothMouseMovement(x_speed, 0, duration)
            local y_speed = 1  -- pixels per millisecond (amount of pixels mouse will move per ms)
            local duration = 40 -- milliseconds (amount of time mouse will move in one direction for)
            SmoothMouseMovement(y_speed, 0, duration)
            local x_speed = -1  -- pixels per millisecond
            local duration = 30  -- milliseconds
            SmoothMouseMovement(x_speed, 0, duration)
            local y_speed = -2  -- pixels per millisecond (amount of pixels mouse will move per ms)
            local duration = 40 -- milliseconds (amount of time mouse will move in one direction for)
            SmoothMouseMovement(y_speed, 0, duration)
            MoveMouseRelative(0,0)
            PressAndReleaseMouseButton(2)
        until not IsMouseButtonPressed(1)
    end
end

编辑后的尝试代码(仍未解决问题)

if IsKeyLockOn("capslock") and IsMouseButtonPressed(1) then
        repeat
            for i = 1, 1 do
                local x_speed = -4  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local y_speed = 1  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local duration = 10 -- milliseconds (amount of time mouse will move in one direction for)
                SmoothMouseMovement(x_speed, y_speed, duration)
            end
            for i = 1, 1 do
                local x_speed = 4  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local y_speed = 1  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local duration = 10 -- milliseconds (amount of time mouse will move in one direction for)
                SmoothMouseMovement(x_speed, y_speed, duration)
            end
            for i = 1, 1 do
                local x_speed = 4  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local y_speed = -1  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local duration = 10 -- milliseconds (amount of time mouse will move in one direction for)
                SmoothMouseMovement(x_speed, y_speed, duration)
            end
            for i = 1, 1 do
                local x_speed = -4  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local y_speed = -1  -- pixels per millisecond (amount of pixels mouse will move per ms)
                local duration = 10 -- milliseconds (amount of time mouse will move in one direction for)
                SmoothMouseMovement(x_speed, y_speed, duration)
            end
            PressAndReleaseMouseButton(2)
        until not IsMouseButtonPressed(1)
        MoveMouseRelative(0,20)
    end
end

解决方案

核心问题是原代码调用SmoothMouseMovement时参数传递错误,且存在冗余代码。以下是修正后的完整实现:

修正后的代码

EnablePrimaryMouseButtonEvents(true)

function SmoothMouseMovement(x_speed, y_speed, duration)
    local last_x, last_y = 0, 0
    local tm_start = GetRunningTime()
    repeat
        Sleep(5)
        if not IsMouseButtonPressed(1) then break end
        local tm = math.min(GetRunningTime() - tm_start, duration)
        -- 计算当前累计移动量
        local current_x = math.floor(x_speed * tm)
        local current_y = math.floor(y_speed * tm)
        -- 仅当移动量变化时执行相对移动
        if current_x ~= last_x or current_y ~= last_y then
            MoveMouseRelative(current_x - last_x, current_y - last_y)
            last_x = current_x
            last_y = current_y
        end
    until tm >= duration
end

function OnEvent(event, arg)
    -- OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
    if IsKeyLockOn("capslock") and IsMouseButtonPressed(1) then
        repeat
            -- 同时移动X左、Y下
            SmoothMouseMovement(-4, 1, 10)
            -- 同时移动X右、Y下
            SmoothMouseMovement(4, 1, 10)
            -- 同时移动X右、Y上
            SmoothMouseMovement(4, -1, 10)
            -- 同时移动X左、Y上
            SmoothMouseMovement(-4, -1, 10)
            
            PressAndReleaseMouseButton(2)
        until not IsMouseButtonPressed(1)
        -- 循环结束后执行一次向下移动
        MoveMouseRelative(0,20)
    end
end

关键修正点

  • 参数传递错误修复:原代码调用SmoothMouseMovement(y_speed, 0, duration)时,错误地将Y轴速度传入X参数位置,第二个参数(Y速度)填0导致无Y轴移动,现在改为同时传入有效X、Y速度值。
  • 移除冗余循环:for i=1,1循环无实际作用,直接调用函数即可。
  • 变量命名优化:将原函数内的x/y改为current_x/current_y,更清晰区分累计移动量与每次增量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 03:15:36