AutoIt暂停/继续脚本功能

时间:2016-03-29 19:15:12

标签: autoit

我正在尝试使用暂停脚本的热键来创建一个函数,因此它不会发送任何内容。但也是一个再次启用脚本的功能。以下是我到目前为止的情况:

Func TogglePause()
   $Paused = NOT $Paused

   If $Paused Then ToolTip('Script "Paused"',500,500)

   While $Paused
      Sleep(1)
   WEnd

   ToolTip("")
EndFunc

假设您正在键入 H ,脚本会暂停,因此它不会执行任何操作。按 J 后,脚本会再次启用。

3 个答案:

答案 0 :(得分:0)

直接来自helpfile(HotKeyset)

#include <MsgBoxConstants.au3>

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{PAUSE}", "TogglePause")
HotKeySet("{ESC}", "Terminate")
HotKeySet("+!d", "ShowMessage") ; Shift-Alt-d

While 1
    Sleep(100)
WEnd

Func TogglePause()
    $g_bPaused = Not $g_bPaused
    While $g_bPaused
        Sleep(100)
        ToolTip('Script is "Paused"', 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>TogglePause

Func Terminate()
    Exit
EndFunc   ;==>Terminate

Func ShowMessage()
    MsgBox($MB_SYSTEMMODAL, "", "This is a message.")
EndFunc   ;==>ShowMessage

这可以解决您的问题吗?

#include <MsgBoxConstants.au3>

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{PAUSE}", "HotKeyPressed")
HotKeySet("1", "HotKeyPressed")
HotKeySet("2", "HotKeyPressed")
HotKeySet("+!d", "HotKeyPressed") ; Shift-Alt-d

While 1
    Sleep(100)
WEnd

Func HotKeyPressed()
    Switch @HotKeyPressed ; The last hotkey pressed.
        Case "{PAUSE}" ; String is the {PAUSE} hotkey.
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "1"
            While 1
                Send('F')
                Sleep(500)
            WEnd

        Case "2"
            While 1
                Send('D')
                Sleep(500)
            WEnd

        Case "+!d" ; String is the Shift-Alt-d hotkey.
            MsgBox($MB_SYSTEMMODAL, "", "This is a message.")

    EndSwitch
EndFunc   ;==>HotKeyPressed

答案 1 :(得分:0)

我再次修改答案以显示我的新例子。每次用户点击回车键时,此示例将在脚本开头发送“Q {ENTER}”。现在,当在键盘上键入字母'b'时,它会自动暂停脚本并像平常一样发送密钥。每当用户键入空格键时,脚本会再次自动激活,以便再次开始发送“Q {ENTER}”。可以使用键盘上的暂停/中断键手动暂停/取消暂停,按退出键即可终止该过程。

Code.au3

; 1 - If the pause/break button is pressed, activate the togglePause function.
; 2 - If the esc (escape) key is pressed, stop the program.
; 3 - If the Enter key is pressed, run your function.


HotKeySet("{PAUSE}", "togglePause")
HotKeySet("{ESC}", "quitScript")
HotKeySet("{ENTER}","Enter")
; ADDED TWO HOTKEYS FOR B AND SPACE
HotKeySet("b", "keyPressB")
HotKeySet("{SPACE}", "keyPressSpace")
; This variable is set to leave the program unpaused at start.
global $isPaused = False
; This empty while loops keeps the program active until manually exiting.
While 1
WEnd

; This is a variation of the function you created.
Func Enter()
        ; If the program is unpaused (which it is at start), the program will send the Q then Enter button.
        if $isPaused == false then
            HotKeySet("{ENTER}");
            Send("Q{ENTER}");
            HotKeySet("{ENTER}","Enter")

        ; If the program is paused, it will send JUST the enter button like you are typing normally.
        elseif $isPaused == true then
            HotKeySet("{ENTER}");
            Send("{ENTER}");
            HotKeySet("{ENTER}","Enter")
        EndIf
EndFunc

; ADDED THE TWO FUNCTIONS BELOW TO PROCESS THE B AND SPACE HOTKEYS
Func keyPressB ()
    ; Pauses the program
    $isPaused = true
    ; Sends the b key like you are normally typing
    HotKeySet("b")
    Send("b")
    HotKeySet("b", "keyPressB")
EndFunc

Func keyPressSpace ()
    ; Unpauses the program
    $isPaused = false
    HotKeySet("{space}")
    Send("{SPACE}")
    HotKeySet("{SPACE}", "keyPressSpace")
EndFunc


; Function to pause script
Func togglePause ()
    ; When this function is initiated, the code on the next line 'toggles' the variable to True/False. If the script is unpaused (the $isPaused variable is set to 'False') then the next line will change it to 'True' and vice versa.
    $isPaused = NOT $isPaused
    ; Create a while loop to stall the program
    ; The line below is the same thing as "While $isPaused == True"
    While $isPaused
        ; This code will run constantly until the $isPaused variable is set to 'True'. To make the script do nothing, simply add a sleep command.
         Sleep(250)
    WEnd
EndFunc

Func quitScript ()
    Exit
EndFunc

答案 2 :(得分:0)

开始 - 没有任何反应。 按f ==&gt;脚本发送f直到 a)按d或ESC a1)按下d ==&gt;脚本发送d直到你按f或ESC a2)按ESC ==&gt;脚本暂停,直到你按下 b1)d ==&gt;发送D. b2)f ==&gt;发送F. b3)ESC ==&gt;发送之前发送的最后一封信。

#include

; Press Esc to terminate script, Pause/Break to "pause"

Global $g_bPaused = False

HotKeySet("{ESC}", "HotKeyPressed")
HotKeySet("f", "HotKeyPressed")
HotKeySet("d", "HotKeyPressed")
HotKeySet("+!d", "HotKeyPressed") ; Shift-Alt-d

While 1
    Sleep(100)
WEnd

Func HotKeyPressed()
    Switch @HotKeyPressed ; The last hotkey pressed.
        Case "{ESC}" ; String is the {PAUSE} hotkey.
            $g_bPaused = Not $g_bPaused
            While $g_bPaused
                Sleep(100)
                ToolTip('Script is "Paused"', 0, 0)
            WEnd
            ToolTip("")

        Case "f"
            $g_bPaused = False
            While 1
                Send('F')
                Sleep(500)
            WEnd

        Case "d"
            $g_bPaused = False
            While 1
                Send('D')
                Sleep(500)
            WEnd

        Case "+!d" ; String is the Shift-Alt-d hotkey.
            MsgBox($MB_SYSTEMMODAL, "", "This is a message.")

    EndSwitch
EndFunc   ;==>HotKeyPressed