1
votes

I just want this loop to work when LButton is down and stop when LButton is up.I The code works when LButton is down but it continues working when i lift my finger off the left click button.

 mem:=0
~*LButton:: 
Sleep, 100
KeyWait, LButton, T0.10
If ErrorLevel = 1
   {
   While GetKeyState("LButton","P")

Loop {
GetKeyState, state, LButton, P
    If state = U
        Break

 MouseGetPos, xpos, ypos

 if (xpos > mem)          ;moved right
    {
     send, {a down}         ;send key
     mem:=xpos
    }
 else
     send, {a up}

 if (xpos < mem)          ;moved left
    {
     send, {d down}
     mem:=xpos
    }
 else
     send, {d up}

  Sleep, 100
}
}
return
1

1 Answers

0
votes

You case is very similar to the example of the usage of the while loop. If you look at that you can see, you don't need the Loop inside the while. Just have

while GetKeyState("LButton")
{
    ; Your code here:
    MouseGetPos, xpos, ypos

    if (xpos > mem)          ;moved right
    {
        send, {a down}         ;send key
        mem:=xpos
    }
    else{
        send, {a up}
    }

    if (xpos < mem)          ;moved left
    {
        send, {d down}
        mem:=xpos
    }
    else{
        send, {d up}
    }

    Sleep, 100
}