罗技G Hub API Lua脚本



我写了一个简单的Lua脚本,点击鼠标5后左键点击。问题是,在第12行,如果我包含"否",它只会重复一次,如果我删除它,它将永远重复。如果可能的话,我想用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

请注意,您的事件处理程序函数在第4行第二次声明。

此外,EnablePrimaryMouseButtonEvents((不必多次调用,也不必对每个输入事件调用——它可以放在只运行一次的函数之外。第一行或最后一行可能是个好主意。

最后,我认为自上而下的条件更为明确。那么,如果你这样做了,给自己一点时间释放按钮并执行一段时间的循环怎么样?如果你在重新检查按钮状态时按住按钮,它会停止循环:

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

最新更新