How to make toggle key to switch between two seperate functions lua script(logitech)

Currently when Caps Lock is on, the script is enabled, and when its off, its disabled. Fairly straightforward, what I'd like to do is when caps is on, all code above else statement is enabled, and when caps is off, code below else statement is enabled. Here is the code, you can see I've got an else statement that should be turning it on when the conditions are not met of the first block, however that doesn't happen. I want the code below the else statement to be used when Caps Lock is toggled off. Also, while I'm on this topic, if anyone knows how i can change the toggle key from Caps Lock to the mouse buttons on the side of my mouse, that would be lovely, as in this case I can just use mouse button 4 for code above the else statement, and mouse button 5 for code below it, would be a huge help.

EnablePrimaryMouseButtonEvents(true);
function OnEvent(event,arg)
    if EnableRCS ~= false then
        if RequireToggle ~= false then
            if IsKeyLockOn(ToggleKey)then
                if IsMouseButtonPressed(3)then
                    repeat
                        if IsMouseButtonPressed(1) then
                            repeat
                                MoveMouseRelative(-1,RecoilControlStrength)
                                Sleep(DelayRate)
                                MoveMouseRelative(0,RecoilControlStrength)
                                Sleep(DelayRate)
                                MoveMouseRelative(-2,RecoilControlStrength)
                                Sleep(DelayRate)
                                MoveMouseRelative(0,RecoilControlStrength)
                                Sleep(DelayRate)
                            until not IsMouseButtonPressed(1)
                        end
                    until not IsMouseButtonPressed(3)
                end
            end

        else 
            if IsMouseButtonPressed(3)then
                repeat
                    if IsMouseButtonPressed(1) then
                        repeat
                            MoveMouseRelative(0,RecoilControlStrength)
                            Sleep(DelayRate)
                        until not IsMouseButtonPressed(1)
                    end
                until not IsMouseButtonPressed(3)
            end
        end
    else 
    end  
end

The top part of the code (before else statement) works as intended, when caps lock is on, that code is running, however I intended for the code following the else statement to take effect when caps lock is off, which is not the case.

Upvotes: 0

Views: 371

Answers (1)

darkfrei
darkfrei

Reputation: 586

The state machine:

sum = function (a, b) return a + b end
sub = function (a, b) return a - b end

States = {sum=sum, sub=sub}

State = States.sum -- current state is sum
c1 = State (5, 4) -- 9

State = States.sub -- and now current state is sub
c2 = State (5, 4) -- 1

Just switch the state and you don't need to check condition, the state is already there.

Upvotes: 0

Related Questions