【问题标题】:Logitech G Hub API Lua Script罗技 G Hub API Lua 脚本
【发布时间】:2021-08-13 00:55:39
【问题描述】:

我写了一个简单的 Lua 脚本,在我单击鼠标后左键单击 5。问题是,在第 12 行,如果我包含“不”,它只会重复一次,如果我删除它,它将永远重复。如果可能的话,我想用 mouse5 开始和结束脚本。我知道人们以前也遇到过类似的问题,但我无法找到解决方案。有什么想法吗?

我正在使用罗技 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

【问题讨论】:

  • 您的代码应该可以正常工作。请注意 G5 鼠标键必须绑定到 GHUB 界面中的“前进”动作。这是默认设置,您可能已经手动更改了。

标签: lua logitech logitech-gaming-software


【解决方案1】:

请注意,您的事件处理函数在第 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))
        -- maybe add another check here "if IMBP(5) then return end"
        PressMouseButton(1)
        Sleep(math.random(27,78)) 
        ReleaseMouseButton(1)
        Sleep(math.random(314,664))
    end end
end

【讨论】:

    猜你喜欢
    • 2021-04-22
    • 2021-09-06
    • 2020-07-25
    • 2021-10-31
    • 2021-08-14
    • 2023-02-10
    • 2021-04-16
    • 2021-04-12
    • 2020-11-02
    相关资源
    最近更新 更多