Rapid fire with logitech lua script?

Ozzie Martin picture Ozzie Martin · Sep 26, 2019 · Viewed 9.4k times · Source

When writing lua scripts for my logitech g502, I've gotten rapid fire to work, but it will continue to execute mouse presses after the mouse one button is released as long as the ctrl key is held. I am wondering if there is any kind of iteration that would allow me to signal a function that presses and releases the mouse but under the conditional that the same mouse button is pressed.(For example, ctrl must be held and rapid fire only executes when mouse button 1 is held down, as opposed to until ctrl is released).

Here is the code I am referring to

EnablePrimaryMouseButtonEvents(true);

function OnEvent(event, arg)
    if IsModifierPressed("lctrl") then
        repeat  
            if IsMouseButtonPressed(1) then
                repeat
                    PressMouseButton(1)
                    Sleep(15)
                    ReleaseMouseButton(1)
                until not IsMouseButtonPressed(1)
            end             
        until not IsModifierPressed("lctrl")
    end         
end

I am wondering if there is any kind of iteration that would allow me to signal a function that presses and releases the mouse but under the conditional that the same mouse button is pressed.(For example, ctrl must be held and rapid fire only executes when mouse button 1 is held down, as opposed to until ctrl is released).

Alternatives I have considered: binding fire to another key that isn't mouse button 1, and having it repeated when mouse button one is pressed.

Thanks in advance

Answer

Egor Skriptunoff picture Egor Skriptunoff · Sep 26, 2019

The actual problem is you're trying to simultaneously read real status and simulate press/release the same mouse button.
The only way to resolve this problem is (as you have suggested) to bind fire to additional key.
For example, in the game config you assign both left mouse button and keyboard key combination CtrlP to "Fire". Please note that P without a modifier must be NOT assigned to any action in the game.
And your script would be the following:

EnablePrimaryMouseButtonEvents(true)

function OnEvent(event, arg)
   if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and IsModifierPressed("lctrl") then
      repeat
         Sleep(15)
         PressKey("P")
         Sleep(15)
         ReleaseKey("P")
      until not IsMouseButtonPressed(1) or not IsModifierPressed("lctrl")
   end
end