autoit

Autoclick script cannot run even though no errors be found


I have construct a script for autoclick when I press F7, but by somehow the script cannot start. Can someone point out for me what I have wrong?

HotKeySet("{f7}","LoopFlagSwitch")
$loopflag = 0

Func autoclick()
    While $loopflag = 1
        MouseClick("left",1171,444,1,5)
        Sleep(400)
        MouseClick("left",666,477,1,5)
        Sleep(400)
        MouseClick("left",1171,499,1,5)
        Sleep(400)
        MouseClick("left",666,477,1,5)      
        Sleep(400)
        MouseClick("left",1171,604,1,5)
        Sleep(400)
        MouseClick("left",666,477,1,5)  
        Sleep(400)
        MouseClick("left",1113,680,1,5)     
        Sleep(700)
    WEnd
EndFunc

Func LoopFlagSwitch()
   If $loopflag == 0 Then
      $loopflag = 1
      autoclick()
   Else
      $loopflag = 0
   EndIf
EndFunc

Solution

  • Your script ends before you are able to press the hotkey. You need to keep the script running, for example with an infinite loop as "main code":

    HotKeySet("{f7}","LoopFlagSwitch")
    $loopflag = 0
    
    While True
        Sleep(100)
    WEnd
    
    Func autoclick()
        ...
    EndFunc
    Func LoopFlagSwitch()
        ...
    EndFunc