How to program a toggle macro in LUA for Logitech Mouse button

2.6k views Asked by At

I'm new to Lua language, just wanna program a macro for Logitech Mouse button, the desired action is: press the button to start and loop the macro continuously until I toggle it off by pressing the button again.

I know a similar question had been post as: Lua handling mouse event . But I have difficulties to run their example code. There are two problems: 1, I can start the loop by pressing button 5, but the while loop only continue by holding the button 5. How can I set the while loop continue by just 1 single press then release? 2, the repeat loop can't be broken when pressing button 5 again. It still keep running continuously.

I also tried this code

script_running = false
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
        if script_running then
            script_running = false
            return
        else
            script_running = true
        end
        repeat
            OutputLogMessage("repeat\n")
            Sleep(3000)
        until not script_running
        OutputLogMessage("end\n")
    end
end

The repeat loop still can't be stopped.

Could someone give some mature code example? Thank you very much.

1

There are 1 answers

0
Egor Skriptunoff On BEST ANSWER
local flag

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 5 then
      flag = not flag
      if flag then
         repeat
            -----------------------
            -- your actions here 
            OutputLogMessage("repeat\n")
            Sleep(1000)
            -----------------------
            Sleep(15)
            local prev_flag = flag
            flag = IsMouseButtonPressed(5)
         until not prev_flag and flag
      end
   end
end