如何停止当前脚本? [英] How to stop the current script?

查看:170
本文介绍了如何停止当前脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个当前脚本,但是即使我在F8处放置了一个Pause脚本,它也不会暂停,有没有办法停止当前循环?

I have this current script but it won't pause, even though i put a Pause script at F8 is there a way to stop the current loop?

F7::
Loop{
Send {1} ; Pressed 1
Send {-}
Send {-}
Send {-}
Send {-}
Send {2} ; Pressed 2
Send {-}
Send {-}
Send {-}
Send {-}
}
F8::Pause

推荐答案

我不知道为什么这完全行不通,我将在这篇文章之后尝试查找,并在发现后对其进行编辑. 但是,该脚本中有很多错误(错误的做法)的内容,因此我们可以轻松为您提供工作版本.

I don't know why exactly that doesn't work, I'll try to find out after this post and will edit that in if I find out. However, there are many things wrong (bad practice) in that script, so we'll be able to get you a working version easily.

正确地,不需要使用多个发送命令.只需将所有字符放在一个发送命令中即可.

Firtly, there is no need to use multiple send commands. Just put all the characters in one send command.

第二,不需要将这些键包装在{}周围.仅当您要转义具有特殊功能的键时才这样做.例如,将!包装在{}中将发送原义的!键而不是ALT键. (在AHK中,!表示ALT修改键)

Secondly, there is no need to wrap those keys around a {}. That's only done when you want to escape keys that have special. For example wrapping ! in {} would send the literal ! key instead of the ALT key. (In AHK ! indicates the ALT modifier key)

好,现在您的脚本如下所示

Ok, so now your script looks like this

F7::
    Loop
        Send, 1----2----
return

F8::Pause

我还删除了循环周围的花括号,单线不需要它们.并在热键末尾添加了return,即使在技术上不需要在这里,但它在这里属于.使用return可以停止代码执行,这就是结束非单行热键语句的方式(单行不需要).
但是,在您的情况下,代码执行将从不逃脱循环,因此这并不重要.

I also removed the braces around the loop, they're not needed for a one-liner. And added the return at the end of the hotkey, even though technically not needed here, but it kind of belongs there. With return you stop code execution, and that's how you end a non-one-liner hotkey statement (not needed for one-liners).
But in your case the code execution will never escape the loop, so it doesn't really matter.

但是您的暂停热键仍然不会中断循环.如果您要使用 SendInput ,它将起作用.因此,只需将Send替换为SendInput.
发送密钥也将非常非常非常快,并且更加可靠.如果速度太快,也可以切换回Send并使用 SetKeyDelay .

But that the loop still doesn't get interrupted by your pause hotkey. If you were to use SendInput it would work. So just replace Send with SendInput.
It would also be very very very much faster at sending the keys and also more reliable. If it's too fast even, you could switch back to Send and tune the speed with SetKeyDelay.

将文本设置到剪贴板并发送ctrl + v甚至比使用发送密钥的任何方式都要好.现在,这是快速而可靠的.但是可能有点多余,因为要发送的字符串不会那么长.但是,如果它变得更长,您肯定会想要这样做.
当然,这是否有效取决于您要向其发送输入的内容.这是一个最小的示例:

Even better than using any way of sending those keys, would be setting the text to your clipboard and sending ctrl+v. Now that's fast and reliable. But maybe a bit excessive since your string to send is not that long. But if it gets longer, you'll surely want to do that.
Of course whether or not that works depends to what you're sending input to. Here's a minimal example for that:

Clipboard := "This is a long string that's going to be sent instantly by the use of pasting it in.`nThis is a long string that's going to be sent instantly by the use of pasting it in."
F7::SendInput, ^v

但是仍然,所有这些都是不好的做法,因为我们正在使用循环.尤其是热键下的循环.

But still, all of this is bad practice because we're using a loop. And especially loop that's under a hotkey.

现在,让我们进入一些更好的方法.但是首先让我说,如果您的整个脚本只是这两个热键,仅此而已,那么这在实践上没有任何实际的区别.但是,为将来参考,当您制作更大更好的脚本时,我想展示一些更好的方法.

Now lets get onto some better ways of doing this. But first let me say that none of this make any actual difference in practice if your whole script is just those two hotkeys and nothing more. But still, for future reference, when you might make bigger and better scripts, I'd like to show some better ways of doing this.

首先,通过暂停,您只需暂停整个脚本(所有循环和计时器等也是如此).但是,您永远不会退出循环.总是在您取消暂停时,循环仍将在那里运行.可能不需要.
您可以使循环包含一些打破循环的条件.满足条件时中断循环的简单快捷示例:

Firstly, by pausing you just pause the whole script (so also all loops and timers, etc). But you never exit the loop. Always when you unpause, the loop will still be running there. Might not be desired.
You could make the loop include some condition under which to break the loop. Quick and minimal example of breaking the loop when a condition is met:

F7::
    BreakLoop := false
    Loop
    {
        if (BreakLoop) ;if true
            break
        ToolTip, % A_TickCount ;built in variable that holds the time, in ms, since our system was started
    }
return

F8::BreakLoop := true

但是实际上,即使使用循环也可能是不好的做法.如果我们有一个更大的脚本,我们希望循环是刚刚在后台运行的东西.但是循环不会那样做.
因此,其次,使用计时器而不是循环.
基本示例:

But actually, even the usage of a loop can be bad practice. If we have a bigger script we'd want the loop to be something that's just running on the background. But a loop wont do that.
So, secondly, using a timer instead of a loop.
Basic example:

F7::SetTimer, OurTimersCallback, 0 ;0 as in execute timer every "0" ms (as fast as possible)
F8::SetTimer, OurTimersCallback, Off ;turn off

OurTimersCallback()
{
    ToolTip, % A_TickCount
}    

甜,但我们可以做得更好.让我们使用相同的热键来打开/关闭,因此我们不需要两个不同的热键:

Sweet, but we can do even better. Lets use the same hotkey to toggle on/off, so we don't need two different hotkeys:

Toggle := true
F7::
    if (Toggle)
    {
        SetTimer, OurTimersCallback, 0
        Toggle := false
    }
    else
    {
        SetTimer, OurTimersCallback, Off
        Toggle := true
    }
return

OurTimersCallback()
{
    ToolTip, % A_TickCount
}

很好,但是不管您相信与否,我们可以做得更好,尽管这部分在实践中没有区别.但是我们可以对热键语句使用单线:

Nice, but believe it or not, we can do even better, though this part makes no difference in practice. But we can use a one-liner for the hotkey statement:

F7::SetTimer, OurTimersCallback, % (Toggle:=!Toggle) ? 0 : "Off"

OurTimersCallback()
{
    ToolTip, % A_TickCount
}

Ph,好吧,那肯定有点令人困惑.请允许我输入解释,说明发生了什么以及为什么起作用.
% ( ) ? <value> : <value> 那是什么?
使用% force可以使我们当前处于表达式语法中的命令的当前参数生效.因此,它可以理解我们正在做的很酷的事情,而不是解释我们按字面意义键入的内容(作为一个字符串).
然后( ) ? <value> : <value>是三元运算符.
如果( )中的语句评估为true,则使用:之前的值(在本例中为 0 ),如果评估为false,则使用:之后的值(在我们的示例中为字符串 Off ).

Phew, ok, well that's surely a bit confusing. Let me type out the explanation for what happens and why it works.
% ( ) ? <value> : <value> So what is this?
With % force the current parameter of command we are in the behave in expression syntax. So instead of interpreting what we type literally (as one string), it understand the cool stuff we're doing.
And then ( ) ? <value> : <value> is a ternary operator.
If the statement inside ( ) evaluates to true, we use the value that's before : (0 in our case), and if it evaluates to false, we use the value that's after : (the string Off in our case).

Toggle:=!Toggle 变量Toggle与其他变量一样,以默认值 开头,在本示例中其值为false,所以我们只说Toggle起始于该值false使事情变得更简单 .

Toggle:=!Toggle The variable Toggle, like any variable, starts off with the default value of nothing, which evaluates to false in this example, so we'll just say Toggle starts off with the value false to keep things a bit more simple.

使用:=我们为Toggle分配一个表达式(简单地说,是一个值(在这种情况下为一个值))(如果您还不知道=:=之间的区别,那是您应该做的事情)学习,但这是另一回事)

With := we assign an expression (simply said, a value (in this case)) to Toggle (if you don't already know the difference between = and :=, that's something you should learn, but that's an other thing)

!Toggle表示与Toggle保持的值相反.
因此,与false(任何变量开头保留的默认值)相反的是true

!Toggle means the opposite of the value Toggle holds.
So opposite of false (the default value any variable holds at the beginning) is true

好,所以现在Toggle拥有值true.
我们的三元声明(Toggle:=!Toggle)正在检查方括号( )中的表达式是否为true.
很好,所以我们在三元组中选择true的值.

Ok, so now Toggle holds the value true.
And our ternary's statement, (Toggle:=!Toggle) is checking if the expression inside the brackets ( ) evaluates to true.
Well it did, so we choose the value for true in our ternary.

下次(再次按下F7时),变量Toggle已经包含值true.
然后,我们再次做同样的事情.我们将!Toggle分配给Toggle.这次相反的值是false,因此if语句的值为false,因此我们在三元数中选择false的值.

Next time around (when we hit F7 again), the variable Toggle already holds the value true.
Then we do the same stuff again. We assign !Toggle to Toggle. This time the opposite value is false, so the if statement evaluates to false, so we choose the value for false in our ternary instead.

然后再次按下F7时,我们再次将Toggle的值更改为相反的值.
Toggle的值在true,false,true,false,true等之间不断变化,这就是我们实现非常好,简短的 toggle 的方式.

And then when F7 is hit once again, we change the value of Toggle to its opposite again.
Toggle's value keeps changing between true, false, true, false, true,... and this is how we achieve a very nice, short, toggle.

很长的帖子,我希望有人能学到一些东西,或者至少觉得有趣. 好吧,至少我喜欢写它.

Very long post, I hope someone learns something, or at least finds it interesting. Well, at least I enjoyed writing it.

这篇关于如何停止当前脚本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆