简体   繁体   English

AHK-在函数内部休眠不起作用

[英]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: 但是,以下代码不会(它完全忽略了Sleep),并且我不确定原因:

time := 1000

<^q:: 
    doKeys()
return

doKeys()
{
    Sleep time
    SendInput {F9}
}

Your first example works because the Variable Time is accessible to the code contained within your Subroutine (gosub) . 您的第一个示例之所以有效,是因为子例程(gosub)中包含的代码可以访问“可变时间”。

Functions 职能

A function is similar to a subroutine (Gosub) except that it can accept parameters (inputs) from its caller. 函数与子例程(Gosub)相似,不同之处在于它可以接受其调用者的参数(输入)。 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. 或者,您可以将变量声明为Global,以便在不将其传递给函数的情况下对其进行访问。

time := 1000

<^q:: 
    doKeys() 
return

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

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. 注意:如果您使用#Warn ,则如果代码中存在类似此类的常见错误 ,则AHK会向您发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM