Jump to content

On and Off switch on a simple script


Go to solution Solved by JohnOne,

Recommended Posts

If my script is a while loop, I've been trying to think of a way to enter and exit the loop at the press of a hotkey, in this case F1 and F2. I have tried 3-4 different methods that I thought of but some just press 2 indefinitely, others don't do anything at all. I know that you can make an Exit function that will completely exit the script, but I want to be able to turn the script back on with just the press of an hotkey and I think if you exit the script you can no longer do that so that's why I'm trying to find a way with Exitloop instead.

This is the last thing I tried (doesn't do anything)

#include <Misc.au3>

Global $ON_OFF = 0

If _IsPressed("F1") Then $ON_OFF = 1 ; OFF

If _IsPressed("F2") Then
    Do
        While 1

            send("{2}")

            If $ON_OFF = 1 Then ExitLoop
        WEnd
    Until $ON_OFF = 1

        $ON_OFF = 0

EndIf

Thanks.

Link to comment
Share on other sites

Firstly: the _IsPressed doesn't work with strings like that, you need to use key codes. The helpfile lists them and gives an example.

Secondly: if you want to indicate a function key in the Send (and related) functions, you would indicate that by "{F1}" instead of "F1". (Not that it makes a difference in this case because _IsPressed doesn't understand either.)

Thirdly: try:

HotKeySet("{F1}", "enterloop")
HotKeySet("{F2}", "quitloop")
HotKeySet("{ESC}", "quitprogram")

Global $quitlooper = True ; Note that we need a global variable if we want two different functions to be aware of its global value

While True
    Sleep(1000)
WEnd

Func enterloop()

    HotKeySet("{F1}") ; unset enterloop hotkey
                      ; makes sure the function will not start over if you press the hotkey while the loop is already running
                      ; comment out if you want that

    $quitlooper = False;

    $timer = TimerInit()

    Do
        ConsoleWrite(TimerDiff($timer) & " ms since loop start..." & @CRLF)
        Sleep(100)
    Until $quitlooper

    HotKeySet("{F1}", "enterloop") ; re-enable enterloop hotkey
EndFunc   ;==>enterloop

Func quitloop()

    $quitlooper = True
EndFunc   ;==>quitloop

Func quitprogram()
    Exit
EndFunc   ;==>quitprogram

Roses are FF0000, violets are 0000FF... All my base are belong to you.

Link to comment
Share on other sites

You looking for a pause function?

 

Yes, I need to pause it with a hotkey then restart then restart script with a hotkey in this case the script is just 1 loop. Basically in my example it sends the letter 2 non stop, if I press F2 it stops sending 2, if I press F1 it restarts, it can also be the same hotkey, F1 start, if I press F1 again it stops, if I press a third time it restarts, etc.

Link to comment
Share on other sites

 

Firstly: the _IsPressed doesn't work with strings like that, you need to use key codes. The helpfile lists them and gives an example.

Secondly: if you want to indicate a function key in the Send (and related) functions, you would indicate that by "{F1}" instead of "F1". (Not that it makes a difference in this case because _IsPressed doesn't understand either.)

Thirdly: try:

HotKeySet("{F1}", "enterloop")
HotKeySet("{F2}", "quitloop")
HotKeySet("{ESC}", "quitprogram")

Global $quitlooper = True ; Note that we need a global variable if we want two different functions to be aware of its global value

While True
    Sleep(1000)
WEnd

Func enterloop()

    HotKeySet("{F1}") ; unset enterloop hotkey
                      ; makes sure the function will not start over if you press the hotkey while the loop is already running
                      ; comment out if you want that

    $quitlooper = False;

    $timer = TimerInit()

    Do
        ConsoleWrite(TimerDiff($timer) & " ms since loop start..." & @CRLF)
        Sleep(100)
    Until $quitlooper

    HotKeySet("{F1}", "enterloop") ; re-enable enterloop hotkey
EndFunc   ;==>enterloop

Func quitloop()

    $quitlooper = True
EndFunc   ;==>quitloop

Func quitprogram()
    Exit
EndFunc   ;==>quitprogram

I've only been learning autoit for 2 days so this is a bit complicated to me, where does my while send 2 loop fits in your code? From my understanding when you press F1 it starts the enterloop func again. Is it like this?

HotKeySet("{F1}", "enterloop")
HotKeySet("{F2}", "quitloop")
HotKeySet("{ESC}", "quitprogram")

Global $quitlooper = True ; Note that we need a global variable if we want two different functions to be aware of its global value

While True
    send("{2}")
WEnd

Func enterloop()

    HotKeySet("{F1}") ; unset enterloop hotkey
                      ; makes sure the function will not start over if you press the hotkey while the loop is already running
                      ; comment out if you want that

    $quitlooper = False;

    $timer = TimerInit()

    Do
        ConsoleWrite(TimerDiff($timer) & " ms since loop start..." & @CRLF)
        Sleep(100)
    Until $quitlooper

    HotKeySet("{F1}", "enterloop") ; re-enable enterloop hotkey
EndFunc   ;==>enterloop

Func quitloop()

    $quitlooper = True
EndFunc   ;==>quitloop

Func quitprogram()
    Exit
EndFunc   ;==>quitprogram

Changing this HotKeySet("{F1}", "enterloop") to this HotKeySet("{F1}") will remove the association between the hotkey and the function so it can not renter the enterloop function while it's inside the loop. F2 hotkey will start the quitloop function which changes the global variable $quitterloop to true. I still find it confusing and don't know where my loop fits in your code lol. I think it's inside the Do loop and F2 changes the quitterloop variable stopping the loop which in turns stop the press of the key.

Edited by mattherat909
Link to comment
Share on other sites

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

Global $Paused = False

While 3
    Sleep(1000)
    If Not $Paused Then
        Send("2")
    EndIf
WEnd

Func _Pause()
    $Paused = Not $Paused
EndFunc

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

SadBunny's code works very well but I don't fully understand it, I will now try your code John. Just wondering are sleep and timers necessary? If you want to make the script as fast as possible and optimize speed how would it affect the timers or do they not have any impact on speed in this specific script?

Link to comment
Share on other sites

SadBunny's code works very well but I don't fully understand it, I will now try your code John. Just wondering are sleep and timers necessary? If you want to make the script as fast as possible and optimize speed how would it affect the timers or do they not have any impact on speed in this specific script?

 

The timers are not necessary, I just put it in to show you what the loop function does and how it reacts to the hotkey being pressed. If you comment out that "unset" thing, you'll see that the timer resets to 0 every time you press the hotkey, even if the loop is already running. So I included an example of how to suppress reacting to the F1 hotkey when the script is busy with the loop :)

You can remove the timer thingies - it's just demo code.

And that While True / Sleep(1000) / Wend block in the middle of the code is just my main loop. It makes the script stay active forever (until you break it off by pressing the {ESC} hotkey). The hotkey doesn't care about the sleep and jumps immediately to the function when pressed, so no, this doesn't make it slower. Because this script is meant to only ever do anything based on hotkey presses, the main loop does not need to do anything else in this case. Of course the sleep in the loop in the "spam function" does make it slower, but that's for the same reason that John indicates - I didn't want it to choke my CPU. Try removing it and see if it still works well enough for you - it may, and it may not. Your mileage may vary :)

Roses are FF0000, violets are 0000FF... All my base are belong to you.

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...