title: How to make this Logitech Mouse LUA script work better?
body: <p>I want to make a simple macro in LUA to Logitech mouse.
  I am complete novice and I just pasted few lines together from few sources.</p>
  <pre class="lang-lua prettyprint-override"><code>EnablePrimaryMouseButtonEvents(true);
  
  function OnEvent(event, arg)
      if IsKeyLockOn(&quot;numlock&quot;) then
          if IsMouseButtonPressed(3) then
              repeat
                  if IsMouseButtonPressed(1) then
                      repeat
                          PressKey(&quot;P&quot;)
                          MoveMouseRelative(0, 13)
                          Sleep(10)
                          MoveMouseRelative(0, 13)
                          Sleep(10)
                          MoveMouseRelative(0, 13)
                          Sleep(10)
                          MoveMouseRelative(0, 13)
                          Sleep(10)
                          MoveMouseRelative(0, 13)
                          Sleep(10)
                          ReleaseKey(&quot;P&quot;)
  
                          Sleep(200)
                      until not IsMouseButtonPressed(1)
                  end
              until not IsMouseButtonPressed(3)
  
          elseif not IsMouseButtonPressed(3) then
              repeat
                  if IsMouseButtonPressed(1) then
                      repeat
                          PressKey(&quot;P&quot;)
                          MoveMouseRelative(0, 15)
                          Sleep(30)
                          MoveMouseRelative(0, 12)
                          Sleep(10)
                          MoveMouseRelative(0, 12)
                          Sleep(10)
                          MoveMouseRelative(0, 12)
                          Sleep(10)
                          MoveMouseRelative(0, 12)
                          Sleep(130)
                          ReleaseKey(&quot;P&quot;)
                          Sleep(200)
                      until not IsMouseButtonPressed(1)
                  end
              until IsMouseButtonPressed(3)
          end
      end
  end
  
  </code></pre>
  <p>First of all, it looks like a mess but kind of works, sorry about it I am complete novice to it.
  Now the problems I am having, is that;</p>
  <ol>
  <li><p>I used numlock to enable or disable the whole thing, because I could not figure out how to turn it ON and OFF with single MouseButtonKey, it either played forever, or I broke the whole thing.
  Which is ok, if not for a weird bug that after I turn off numlock I have to press MouseButton 2 to turn it off for some reason.</p>
  </li>
  <li><p>I thought that what I want to achieve would be really simple,
  I want to have 4 functions that;</p>
  </li>
  </ol>
  <pre><code>Function 1 MoveMouseRelative repeated every ()ms
  Function 2 MoveMouseRelative repeated every ()ms
  Function 3 PressKey(&quot;P&quot;) repeated every ()ms
  Function 4 PressKey(&quot;P&quot;) repeated every ()ms
  </code></pre>
  <p>And combine it that so
  If I press MouseButtonPressed(1) it does Function 1+3, so it clicks P and drags mouse down at the same time, but with different sleep() intervals that would be stated in separate functions
  and else if I press and MouseButtonPressed(3)(Right click) + MouseButtonPressed(1) it would do 2+4 functions instead, and would repeat while holding and stop repeating if I released buttons.</p>
  <p>Is that even possible? I got sucked into this code and can't stop thinking how to fix for last week, but having no education and simply guessing how thing would play out I hit a wall I cannot breach.</p>
  <p>Thank you very much for any help if all.</p>