Jump to content

Toggle script on and off


trost
 Share

Recommended Posts

Looking to be taught how to create a script with a toggle on/off function utilizing one key stroke.

Autohotkey has the following script to toggle a script on/off:

f12::

Pause

Suspend

Return

How do I accomplish this with AutoIt?

Thanks

Trost

Link to comment
Share on other sites

Look at HotKeySet in the help file


Time you enjoyed wasting is not wasted time ......T.S. Elliot
Suspense is worse than disappointment................Robert Burns
God help the man who won't help himself, because no-one else will...........My Grandmother

Link to comment
Share on other sites

So this is what I found under help for HotKeySet

Global $Paused

HotKeySet("{PAUSE}", "TogglePause")

HotKeySet("{ESC}", "Terminate")

HotKeySet("+!d", "ShowMessage") ;Shift-Alt-d

I have very little understanding of scripts and how to manipulate the commands. How can I adapt this to start the script, as I can see where the terminate function comes from?

Lastly, I applied this line to my current script to see if the pause and terminate functionality worked and it did not.

Trost

Link to comment
Share on other sites

Because there are no functions to match them.

Try adding this

Global $Paused = False ;set var top of script
HotKeySet("{PAUSE}", "TogglePause") ;top of script
Func TogglePause() ; anywhere in script
$Paused = Not $Paused ; change the state of the var
While $Paused
  Sleep(100)
WEnd
EndFunc

or try this by itself

Global $Paused = False ;set var top of script
HotKeySet("{PAUSE}", "TogglePause") ;top of script
Func TogglePause() ; anywhere in script
$Paused = Not $Paused ; change the state of the var
While $Paused
  Sleep(100)
WEnd
EndFunc   ;==>TogglePause
While 1
ConsoleWrite(123 & @CRLF)
Sleep(1000)
WEnd
Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

I think I am going to spend some time with the AutoIt Absolute Beginners link at the bottom of your posts. I've inserted the lines where I think they should go, but many of them are duplicated with my existing script (which I did not write). When I have a better understanding I guess i'll head back. Thanks for the help so far though.

Link to comment
Share on other sites

here is a heavily commented description of what is going on.

Global $Paused = False
;This is a global variable
;It is called global because it is
;not inside a function or loop
;and can be accessed from anywhere in your script

HotKeySet("{PAUSE}", "TogglePause")
;This is an Autoit native function
;that sets a key to execute a named function
;when that key is pressed
;In this case the key is the "PAUSE" key
;and the function is named "TogglePause" (below)


Func TogglePause() ; anywhere in script
;This is the function that is executed when you hit the HotKey PAUSE
;First it sets the variable $Paused to Not $Paused
;Since we set it to False at the top of the script
;It will become True, and the script will enter the loop
;which just sleeps, idles or does nothing.
;When you hit the HotKey PAUSE again , the variable $Paused
;becomes False and the script exits the loop and continues
;your script from where it left off
$Paused = Not $Paused
While $Paused = True
  Sleep(100)
WEnd
EndFunc   ;==>TogglePause

While 1
;This is just to demonstrate how the pause works
;When you run this script, it will write
;123
;123
;123
;etc..
;to the console in scite4autoit
;normally this is where the main loop of your script should be
;And when you hit the pause button on your keyboard the script will stop
;until you hit it again
ConsoleWrite(123 & @CRLF)
Sleep(1000)
WEnd

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...