Assigning Lua Script to a Button - Logitech G500s

LukeJ picture LukeJ · Oct 29, 2014 · Viewed 7.8k times · Source

I have a small macro for my Logitech G500s - i'm using it in FPS games to reduce recoil. Please see script below:

EnablePrimaryMouseButtonEvents(true)
function OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 1  then
        repeat
            MoveMouseRelative(-1,2)
            Sleep(16)
        until not IsMouseButtonPressed(1)
    end
end

Problem is that this script is works constantly. I wish rather to push another button1 to start using script on button2 and re-press button1 to break script

I was trying to set up the flags such as:

unction OnEvent(event, arg)
    if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then --set flag for mb1
                mb1_pressed = true
        elseif event == "MOUSE_BUTTON_RELEASED" and arg == 6 then --set flag for mb1=false
        mb1_pressed = false
        end 

If mb1_pressed then
    if event == "MOUSE_BUTTON_PRESSED" and arg == 1 and  then
        repeat
            MoveMouseRelative(-1,2)
            Sleep(16)
        until not IsMouseButtonPressed(1)
    end

end  

but it does not work. Could you please help me ?

Answer

Danil Gaponov picture Danil Gaponov · Oct 30, 2014
 if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then --set flag for mb1
            mb1_pressed = true
 elseif event == "MOUSE_BUTTON_RELEASED" and arg == 6 then --set flag for mb1=false
    mb1_pressed = false
 end

This will set mb1_pressed when mouse button is pressed and unset it when it is released. So, the variable will only be true while the button is pressed.

If you want to toggle the mb1_pressed value on each press, you can do something like

if event == "MOUSE_BUTTON_PRESSED" and arg == 6 then --toggle flag for mb1
            mb1_pressed = not mb1_pressed
end