Reputation: 11
I wrote a simple Lua script that left clicks after I click mouse5. The issue is, on line 12 if I include 'not' it will only repeat once, if I remove it, it will repeat forever. I want to start and end the script with mouse5 if possible. I know people have had similar issues to mine before, but I was unable to find a solution. Any ideas?
I am using the Logitech G Hub API: (https://douile.github.io/logitech-toggle-keys/APIDocs.pdf)
And this for my loop: (https://www.tutorialspoint.com/lua/lua_repeat_until_loop.htm)
My code:
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
Upvotes: 0
Views: 8992
Reputation: 4048
Notice your event handler function is declared a second time within itself on line 4.
Also, EnablePrimaryMouseButtonEvents() does not have to be called multiple times or with each input event - it can be placed outside the function where it will run just once. Something like the first or last line is probably a good idea.
Finally, I would argue that top-down conditions are a bit more clear. So how about giving yourself a tiny bit of time to release the button and perform a while loop if you do. If you hold the button during the re-check of the button state, it will stop the loop :
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
Upvotes: 0