Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AHK - Sleep inside a function doesn't work

The following code works as expected:

time := 1000

<^q::
    Sleep time
    SendInput {F9}
return

However, the following code does not (it ignores Sleep entirely), and I'm unsure as to why:

time := 1000

<^q:: 
    doKeys()
return

doKeys()
{
    Sleep time
    SendInput {F9}
}
like image 242
Edge Avatar asked May 29 '26 07:05

Edge


2 Answers

Your first example works because the Variable Time is accessible to the code contained within your Subroutine (gosub).

Functions

A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller. In addition, a function may optionally return a value to its caller.

time := 1000

<^q:: 
    doKeys(time) ; Pass your variable to the function
return

doKeys(x) ; Set your function to accept a variable
{
    Sleep x
    SendInput {F9}
}

Alternatively you could declare a variable as Global so that it is accessible without passing it to the function.

time := 1000

<^q:: 
    doKeys() 
return

doKeys() 
{
    global time
    Sleep time
    SendInput {F9}
}
like image 120
errorseven Avatar answered Jun 01 '26 04:06

errorseven


Make the variable global:

time := 1000

<^q:: 
    doKeys()
return

doKeys()
{
    global time
    Sleep time
    SendInput {F9}
}

Note: If you use #Warn, AHK will give you a warning if there are common errors like this in the code.

like image 29
Schneyer Avatar answered Jun 01 '26 02:06

Schneyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!