0
votes

I have a simple color clicker code.

    ~C:: 
    Loop
    {
        PixelSearch, Px, Py, 300, 300, 300, 300, 0x91595c, 5, Fast
       if errorlevel
       {
          sleep 20
          return
       }
       else
       {
        Send, {LButton Down}
        Sleep, 250
        Send, {LButton Up}
        Sleep, 500
       } 
    }
    return

When pressing a C, its find a right color and send a click. But i tired to make it work ONLY when C is pressed and finish color search when C is not pressed.

What have you already tried? I tried to make this work with:

while  KeyIsDown := GetKeyState("c", "P")

or

if  KeyIsDown := GetKeyState("c")

But it just come to infinite loop or doesnt work. Can you have any ideas?

2

2 Answers

0
votes

Find a solution:

 $F2:: 
    Loop
    {
        PixelSearch, Px, Py, 300, 300, 300, 300, 0x91595c, 5, Fast
       if errorlevel
       {
          sleep 20
          return
       }
       else
        if GetKeyState("C","p") 
       {
        Send, {LButton Down}
        Sleep, 250
        Send, {LButton Up}
        Sleep, 500
       } 
    }
    return

Glad to see if you have a better solutions.

0
votes

If I understand you right, you want to keep the looping going until you release C. If so, you shouldn't return inside the loop, for that will stop it.

$F2:: 
Loop
{
    PixelSearch, Px, Py, 300, 300, 300, 300, 0x91595c, 5, Fast
   if errorlevel
   {
      sleep 20
   }
   else
    if GetKeyState("C","p") 
   {
    Send, {LButton Down}
    Sleep, 250
    Send, {LButton Up}
    Sleep, 500
   } 
   else
     break ; C was released: exit the loop, thus exit the thread
}
return

Also, you can keep ~C as the hotkey, and if you really only want to "click", there is no need for a complicated {LButton down} etc.

Suggested code (also more compact):

~c::
    while(getKeyState("C","P)) {
        PixelSearch, Px, Py, 300, 300, 300, 300, 0x91595c, 5, Fast
        if( ! errorLevel ) {
            click
            sleep, 750
        } else {
            sleep 20
        }
    }
return