Search code examples
autohotkey

AHK Toggle on and off


So I have this script :

~$LButton::
    KeyWait LButton, T0.0
    If ErrorLevel
        While GetKeyState("LButton", "P"){
            Click
            Sleep 0
        }

And I want it to be on if I press F3 and to be off if I press F3 or something else like F4.Like a toggle script on and off.Disable and Enable.So for example,F3 I use the script,I want to do something else F3,I come back and want to use the script F3.And so on. I am new to scripting so if someone could help me that would be nice,thank you. I tried to make it myself but the most I got was press designated key then script runs,press click then script stops,but I don't want it to stop I want it to constantly run until I press the special key,and be stopped until I press the special key again.

I tried to make a simple script but I want to be capable to turn it on and off how many times I need to. So I want someone to help me do my script and tell me how to do the toggle on and off.And I want someone to show me what I am missing and how is done.


Solution

  • Another option... just a basic toggle you can use as needed.

    #MaxThreadsPerHotkey 4   ; Sets the maximum number of simultaneous threads per hotkey.
    #NoEnv           ; Avoids script checking empty variables.
    #Persistent          ; Keeps a script running until ExitApp or user closes it.
    #SingleInstance Force    ; Attempts to launch an already-running script are ignored.
    SetBatchLines -1         ; Run script at maximum speed.
        
        F3:: 
            Toggle := !Toggle
                if (Toggle = 1) {
                    Soundbeep, 1900, 300
                    MsgBox, 48,,Toggle Is`nActive., 3
        ;;  Put Your Active Code Here.
            } else {
                    Soundbeep, 1500, 300
                    MsgBox, 262208,,Toggle Is`nNOT`nActive., 3
        ;;  Put Other Code Here, Or A Return If You Want Nothing Else To Happen.
            }
        Return