Christian
Christian

Reputation: 26427

How do I create while loop in autohotkey that breaks when I press {Ctrl}?

I have the following while-loop in autohotkey inside a function:

foo(){
    counter:=1
    while(counter<10)
    {    
    send, %counter%
    Random, SleepAmount, 2300, 3300
    sleep, 3000
    counter++
    }
}

I want the ability to stop the loop by pressing {Ctrl}. What's the best way to accomplish my goal?

Upvotes: 1

Views: 2391

Answers (2)

juan
juan

Reputation: 1

~Ctrl::counter := 10
F1:: foo()

foo(){
    global counter:=1
    while(counter<10)
    {    
    send, %counter%
    Random, SleepAmount, 2300, 3300
    sleep, SleepAmount
    counter++
    }
}

Upvotes: 0

Relax
Relax

Reputation: 10603

Try it this way:

F1:: foo()

foo(){
    counter := 1
    while(counter < 10)
    { 
        send, %counter%
        Random, SleepAmount, 23, 33     
        loop 100
        {
            Sleep, %SleepAmount%
            If GetKeyState("Ctrl", "P") 
                return
        }
        counter++
    }
}

Upvotes: 2

Related Questions