Jump to content
Sign in to follow this  
frew

How to stop a script or pause a script with a key combination?

Recommended Posts

frew

Hello,

I would like to be able to stop a script or pause a script with a key combination.

I read the help, searched the forum a bit, and checked the FAQ at

http://www.autoitscript.com/autoit3/docs/faq.htm#12

...but I would love to have a simple way to pause a script then resume it after a little while

...and to be able to just stop a script with a key combination..without having to put some code into

each individual script...sort of like a global setting to just press a couple keys to stop it?

...because I do a lot of mouse movement scripts and when the mouse gets moving all around I cannot get down to

the tray area to pause or stop the script.

Thanks for any ideas about this.

Share this post


Link to post
Share on other sites
Bert

well, with most scripts, unless you specify otherwise, you can click on the icon in the system tray to pause. I've seen a UDF somewhere that you can get the info and IDs from the icons in the tray. I've looked, but I can't remember it's name.

Share this post


Link to post
Share on other sites
SwordHunter4Ever

HotkeySet("{HOME}","_end")
Func _end()
Exit 0
EndFunc

;Script here

Exit

Its a very basic method... but it works =]

Thats stops the script, pausing I haven't figure out a method yet. I suppose you could just replace _end with _pause

Share this post


Link to post
Share on other sites
FreeFry

Something, that will pause your script, which can also be copy pasted into probably any script, could look something like this:

HotKeySet("{pause}", "_Pause"); top of script

Dim $i = 1; Demonstration purpose

While 1
    Sleep(250); Demonstration purpose
    ConsoleWrite($i & @LF); Demonstration purpose
    $i += 1; Demonstration purpose
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    ConsoleWrite("isPaused: " & $isPaused & @LF)
    While $isPaused
        Sleep(250)
    Wend
EndFunc

When the pause key on your keyboard is pressed, the function will set the $isPaused variable to True, and then run the While loop in the _Pause function, until it's run again. :P

Edit:

Removed the global variable needed to be set at the top of the script, the _Pause function now handles that on it self. :P

Edited by FreeFry

Share this post


Link to post
Share on other sites
frew

Thanks for the ideas to try but I cannot seem to get them to work.

I pasted them into the top of my script, and with the stop script code above, the script would not run at all.

Edit: pasted one at a time, testing each one separately

With the pause script code above, the script would start and immediately be paused automatically, and any attempt

to unpause it from the tray does not unpause it...like it's stuck in pause.

I'm probably doing something wrong, I'm very new to this. So far I just do very basic scripting of mouse movement and mouse clicks...and running files and programs, but nothing more than that yet.

Any ideas for me to try, or anything I should check to get these running.

By the way, I'm using Windows98SE, so would that be a factor?

Thank you.

Edited by frew

Share this post


Link to post
Share on other sites
FreeFry

Did you try my example on it self? You only need to put the function in there somewhere, then add the HotKeySet part at the top of your script.(you also need an infinite loop, like in my example, so the script wont turn off instantly). :P

Share this post


Link to post
Share on other sites
frew

Sorry, I'm still not getting it. I'm just learning the basics.

I want to be able to start a script, press a key (let's say the pause key) at any time to pause the script, then press the pause key again to resume the script. Like setting up the pause key to be a play/pause button.

Below is a copy of your code until the MouseMove material below it.

Should I now be able to just press the pause key to toggle pause/play as the script runs?

Is this below an example of how to set up it the script?

HotKeySet("{pause}", "_Pause"); top of script

Dim $i = 1; Demonstration purpose

While 1
    Sleep(250); Demonstration purpose
    ConsoleWrite($i & @LF); Demonstration purpose
    $i += 1; Demonstration purpose
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    ConsoleWrite("isPaused: " & $isPaused & @LF)
    While $isPaused
        Sleep(250)
    Wend
EndFunc



MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Edited by frew

Share this post


Link to post
Share on other sites
FreeFry

Sorry, I'm still not getting it. I'm just learning the basics.

I want to be able to start a script, press a key (let's say the pause key) at any time to pause the script, then press the pause key again to resume the script. Like setting up the pause key to be a play/pause button.

Below is a copy of your code until the MouseMove material below it.

Should I now be able to just press the pause key to toggle pause/play as the script runs?

Is this below an example of how to set up it the script?

HotKeySet("{pause}", "_Pause"); top of script

Dim $i = 1; Demonstration purpose

While 1
    Sleep(250); Demonstration purpose
    ConsoleWrite($i & @LF); Demonstration purpose
    $i += 1; Demonstration purpose
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    ConsoleWrite("isPaused: " & $isPaused & @LF)
    While $isPaused
        Sleep(250)
    Wend
EndFunc



MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
Sleep(2000)
MouseMove(900,600)
Sleep(2000)
MouseMove(600,600)
No, that's wrong, you obviously need to put the code which you want to run before, or inside the infinite loop (While 1 is an infinite loop).

HotKeySet("{pause}", "_Pause")

While 1
    MouseMove(900,600)
    Sleep(2000)
    MouseMove(600,600)
    Sleep(2000)
    MouseMove(900,600)
    Sleep(2000)
    MouseMove(600,600)
    Sleep(2000)
    MouseMove(900,600)
    Sleep(2000)
    MouseMove(600,600)
    Sleep(2000)
    MouseMove(900,600)
    Sleep(2000)
    MouseMove(600,600)
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    While $isPaused
        Sleep(250)
    Wend
EndFunc

Assuming that you want the MouseMove stuff to happen over and over again.

Share this post


Link to post
Share on other sites
frew

Okay I think I have it.

Let me know please if this looks right.

I did some tests and it appears to work well as described below.

I'm new to this so I just supplied the simple mouse movements to use for simple testing of your code.

I put in some comments for any others new to this who may like to try it.

Thanks so much FreeFry for your help.

;this is code is to enable using the pause key to pause and resume a script that's running



;this version allows to pause the script and then resume the script from where it was paused
;by using the pause key, and when the script comes to it's end it stops


HotKeySet("{pause}", "_Pause")



    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    
    
While 1
    
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    While $isPaused
        Sleep(250)
    Wend
EndFunc




;this version also allows to pause the script and then resume the script from where it was paused
;by using the pause key
;but this time when the script comes to it's end it starts all over again and loops indefinitely
;until it is stopped manually by pausing it with the pause key and clicking exit from the tray icon




HotKeySet("{pause}", "_Pause")


While 1

    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    Sleep(1000)
    MouseMove(900,600)
    Sleep(1000)
    MouseMove(600,600)
    
WEnd

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    While $isPaused
        Sleep(250)
    Wend
EndFunc

EDIT: The top version has the mouse movement portion of the script put in before the While part, and the lower version has mouse movement portion of the script put in after the While part. ( I guess that's the While loop you refer to...I'm new to programming). Thank you.

Edited by frew

Share this post


Link to post
Share on other sites
FreeFry

The first section of your code(please separate them in the future, to reduce confusion. :P) will first run your MouseMove code(and pause if the pause button is pressed), then when it hits the While 1 loop, it will loop indefinitely(ie. never exit), and it will also make your cpu go 100%(because there's no sleep in that loop).

The second script will run your MouseMove code over and over, without ever exiting.

If you wanna make the script exit after it's done the MouseMove action, the remove the While 1 and WEnd part in the first script of yours. :P

Share this post


Link to post
Share on other sites
frew

Okay, I think I get it now.

This code that you gave:

HotKeySet("{pause}", "_Pause")

needs to be put into the top of the script so that the pause key is set as the key that will toggle

pause/resume of the script.

This code that you gave:

Func _Pause()
    If Not IsDeclared("isPaused") Then Global $isPaused = False
    $isPaused = Not $isPaused
    While $isPaused
        Sleep(250)
    Wend
EndFunc

is the code that enables the pausing to occur, and it can be placed anywhere in the script.

This code that you gave:

While 1
    
WEnd

is the While loop, and any script placed between the While 1 and the WEnd will loop over and over indefinitely

until the script is stopped manually. (I stop the script manually in this case by pressing the pause key, then going

AutoIt3 tray icon and right click> exit).

I think that's it. (I'm new to this so it takes me a while to get the details all clearly understood.)

Thanks so much for your help with this.

Share this post


Link to post
Share on other sites
FreeFry

Hehe, that's exactly how it works. :P

Well everyone has to start some somewhere, and we all learn new things every day. ^^

Just a side-note though, the While 1 WEnd is not really necessary, only if you want the script to loop. :P

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×