Logitech G Hub API Lua Script

我写了一个简单的Lua脚本,在我点击mouse5后进行左键单击。 问题是,如果在第12行包括'not',它只会重复一次;如果将其删除,则会永远重复。如果可能的话,我希望以mouse5开始和结束脚本。 我知道有人之前遇到过类似的问题,但我找不到解决方法。 有什么想法吗?

我正在使用Logitech G Hub API: ( https://douile.github.io/logitech-toggle-keys/APIDocs.pdf)

以及这个用于我的循环: ( https://www.tutorialspoint.com/lua/lua_repeat_until_loop.htm)

我的代码:

function OnEvent(event, arg)
    OutputLogMessage("Event: "..event.." Arg: "..arg.."\n")
EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
        repeat
            Sleep(math.random(1046, 1292))
            PressMouseButton(1)
            Sleep(math.random(27, 78))
            ReleaseMouseButton(1)
            Sleep(math.random(314, 664))
        until not IsMouseButtonPressed(5)
        end
    end
end
点赞
用户2117697
用户2117697

注意你的事件处理函数在第4行内部被再次声明。

此外,EnablePrimaryMouseButtonEvents() 不需要在每个输入事件上被多次调用 - 它可以放在函数外面,只需在函数运行一次即可。像第一行或者最后一行一样放置一下可能是个好主意。

最后,我认为自上而下的条件判断更加清晰。那么,如果你需要一点时间松开按钮并执行 while 循环,可以这样做。如果在重新检查按钮状态期间按住按钮,它将停止循环:

EnablePrimaryMouseButtonEvents(true)
function OnEvent(e,a)
    OutputLogMessage("Event: "..e.." Argument: "..a.."\n")
    if e == "MOUSE_BUTTON_PRESSED" and a == 5 then
        Sleep(200)
        while not IsMouseButtonPressed(5) do
            Sleep(math.random(1046,1292))
            -- 可以在这里添加另一个检查 "if IMBP(5) then return end"
            PressMouseButton(1)
            Sleep(math.random(27,78))
            ReleaseMouseButton(1)
            Sleep(math.random(314,664))
        end
    end
end
2021-08-29 08:15:40