Jump to content

Return from any function using HotKey


Kohr
 Share

Recommended Posts

I wanted to know if there is an easy way to set a hotkey that when pressed will exit all function (no matter which one the code is executing) and return back to the program GUI. The only way I can think of now is to set some global variable then perform a test on the variable in every function to perform the "return".

One of my scripts uses several functions and each function may/may not have loops. I currently just use a hotkey to exit but I have a console writing ability on my gui and I wanted to review this without forcing the program to exit.

The attached example shows how once you hit the "Start" button you end up in the "Code()" function. I know this only simulates the code executing in the same function but I wanted it to stop no matter which function it is in (imagine lots of functions and the code step could be in any of them) without using "Exit".

I guess what I need is some type of global return instead of using "Exit".

Kohr

#include <GUIConstants.au3>

HotKeySet("{HOME}", "CloseClicked")
HotKeySet("{PAUSE}", "Pause")

Global $gPaused

GUICreate("G1", 300, 200)
$btnStart = GUICtrlCreateButton("Start", 10, 10, 50, 20)
GUICtrlSetOnEvent($btnStart, "Start")


GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseClicked")
While 1
    Sleep(200)
WEnd

Func Start()
    $i = 0
    While 1
        Code()
        If $i = "some exit condition" Then
            Return
        EndIf
    WEnd
EndFunc   ;==>Start

Func Code()
    $i = 0
    While 1
        ;Executes here
        If $i = "some exit condition" Then
            Return
        EndIf
    WEnd
EndFunc   ;==>Code

Func Pause()
    $gPaused = Not $gPaused
    While $gPaused
        Sleep(300)
        ToolTip('paused.' & @CRLF & @CRLF, 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>Pause
Func CloseClicked()
    Exit
EndFunc   ;==>CloseClicked
Link to comment
Share on other sites

The only way I can think of now is to set some global variable then

perform a test on the variable in every function to perform the "return".

one way to do it! The other would be: Don't use infinite loops in

your functions!

Cheers

Kurt

__________________________________________________________(l)user: Hey admin slave, how can I recover my deleted files?admin: No problem, there is a nice tool. It's called rm, like recovery method. Make sure to call it with the "recover fast" option like this: rm -rf *

Link to comment
Share on other sites

Here is a working example if anyone cares. The main thing is the 1st function I had to use "ExitLoop" vs. using "Return" in all the other functions.

I did notice that it did have to perform a "Return" every time for each function (example goto func 1, func 2, func 1, func 2. That would perform 4 returns). It is almost like it has to "back out" of the previous calls. That is above what I know so if anyone can explain this to me I would appreciate it. Thanks.

Kohr

#include <GUIConstants.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{END}", "CloseClicked")
HotKeySet("{PAUSE}", "Pause")
HotKeySet("{HOME}", "GlobalReturn")

Global $gPaused
Global $gReturn
Global $gCodeUsedCount
Global $gCodeUsed2Count

GUICreate("G1", 300, 200)
$btnStart = GUICtrlCreateButton("Start", 10, 10, 50, 20)
GUICtrlSetOnEvent($btnStart, "Start")

GUISetState()
GUISetOnEvent($GUI_EVENT_CLOSE, "CloseClicked")
While 1
    Sleep(200)
    $gReturn = "false"
    ;ConsoleWrite($gReturn & @CRLF)
WEnd

Func Start()
    While 1
        ConsoleWrite("Start Function" & @CRLF)
        Code()
        If $gReturn = "true" Then
            ConsoleWrite("Perform ExitLoop" & @CRLF)
            ExitLoop
        EndIf
    WEnd
EndFunc   ;==>Start
Func Code()
    $i = 0
    $gCodeUsedCount += 1
    While 1
        $i += 1
        Sleep(100)
        ConsoleWrite("Code Loop - Count:" & $gCodeUsedCount & @CRLF)
        If $i > 10 Then
            $i = 0
            Code2()
        EndIf
        If $gReturn = "true" Then
            ConsoleWrite("Perform Return" & @CRLF)
            Return
        EndIf
    WEnd
EndFunc   ;==>Code
Func Code2()
    $i = 0
    $gCodeUsed2Count += 1
    While 1
        $i += 1
        Sleep(100)
        ConsoleWrite("Code2 Loop - Count:" & $gCodeUsed2Count & @CRLF)
        If $i > 10 Then
            $i = 0
            Code()
        EndIf
        If $gReturn = "true" Then
            ConsoleWrite("Perform Return" & @CRLF)
            Return
        EndIf
    WEnd
EndFunc   ;==>Code
Func Pause()
    $gPaused = Not $gPaused
    While $gPaused
        Sleep(300)
        ToolTip('paused.' & @CRLF & @CRLF, 0, 0)
    WEnd
    ToolTip("")
EndFunc   ;==>Pause
Func GlobalReturn()
    $gReturn = "true"
    ConsoleWrite($gReturn & @CRLF)
EndFunc   ;==>GlobalReturn
Func CloseClicked()
    Exit
EndFunc   ;==>CloseClicked
Link to comment
Share on other sites

I am also trying to figure out a way to do this

Cue,

Look at how I am using $gReturn in my example code. You will have to do a test for it after every "Sleep" command in your example post.

I am using this because I have a bot that reports info to my gui so I could watch the bot and see what function was being executed. Writing to the console is also available in my script but I can see more lines of data the way I am reporting back to my gui. The hotkey was just a way I needed to exit the bot and go back to the gui to see what has been done.

Kohr

Link to comment
Share on other sites

Iam not sure i follow, but in my script after every sleep() i test $PressfNOTfinished which i think does the same thing.

only problem is:

say i call the function about 20 times will it not test it 20 times? I was wondering if there was a more efficient way.

I have posted my script here for convenience

#include <GUIConstants.au3>
$window_h=0
$PressfNOTfinished=0
HotKeySet("1","keypressed")
While 1
    sleep(20)
WEnd
Func keypressed()
   
    $PressfNOTfinished=1
   
    GUIDelete($window_h)
    $window_h=GUICreate ( "test", 300, 300, 0, 0, $WS_POPUPWINDOW, $WS_EX_TOPMOST + $WS_EX_TOOLWINDOW )
    GUISetState(@SW_SHOW,$window_h)
    GUISetBkColor(0x0000FF,$window_h)
    Sleep(2000)
   
    if $PressfNOTfinished Then
       
        GUISetBkColor(0x00FF00,$window_h)
        Sleep(2000)
       
        If $PressfNOTfinished Then
           
            GUISetBkColor(0xFF0000,$window_h)
            Sleep(2000)
           
        EndIf
       
    EndIf
   
    $PressfNOTfinished=0
EndFunc
Edited by Cue
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...