title: How to program a toggle macro in LUA for Logitech Mouse button
body: <p>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.</p>
  <p>I know a similar question had been post as:
  <a href="https://stackoverflow.com/questions/49142618/lua-handling-mouse-event">Lua handling mouse event</a> . 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.</p>
  <p>I also tried this code</p>
  <pre><code>script_running = false
  function OnEvent(event, arg)
      if event == &quot;MOUSE_BUTTON_PRESSED&quot; and arg == 5 then
          if script_running then
              script_running = false
              return
          else
              script_running = true
          end
          repeat
              OutputLogMessage(&quot;repeat\n&quot;)
              Sleep(3000)
          until not script_running
          OutputLogMessage(&quot;end\n&quot;)
      end
  end
  </code></pre>
  <p>The repeat loop still can't be stopped.</p>
  <p>Could someone give some mature code example? Thank you very much.</p>