Jump to content

Navigating through functions


IAMK
 Share

Recommended Posts

Hello,

 

I need to be able to jump from function to function (not simply go inside it). I have two hotkeys, "1" and "2". If I press "1", it jumps to the previous function. If I press "2", it jumps to the next function. The functions look something like this:
func1()

func2()

....

func50()

 

I have an integer variable to record which function it is at "$currentFunc" (starting from 1).

 

How do I make it so that when the hotkey is pressed, the program goes to the next/previous function and does not continue the function it was in before pressing the hotkey?

 

Also, some functions use RunWait(otherProgramName.exe), so when I navigate away from the function, I need to kill this exe. How do I achieve this (if possible)?

Link to comment
Share on other sites

Slightly confused with what you're looking for. Maybe this will help? 

HotKeySet("{F1}", "_Terminate")
HotKeySet("1", "_FirstFunction")
HotKeySet("2", "_SecondFunction")

While 1                                 ;Makes it so script never dies
   Sleep(1000)
WEnd

Func _Terminate()                       ;Press F1 to end script completly
   Exit
EndFunc

Func _FirstFunction()
   MsgBox(0,"","Example function 1.",1) ;Stays open for 1 second but remains active
EndFunc

Func _SecondFunction()
   MsgBox(0,"","Example function 2.",1) ;Stays open for 1 second but remains active
EndFunc

 

Link to comment
Share on other sites

@badcoder123

Sorry, I tried to make it as simple to understand as possible without showing code.

The hotkeys are fine. There is no While. It will execute from func1->func50 and then the script is finished.

A function will not move from func1->func2 until "2" is pressed. (I'm thinking of just putting Sleep(600000) at the end of each function since the user will be operating the script)

Func Func1()
    $x = 5
    Sleep(1000)
    $y = 10
    Sleep(1000)
    $z = 15
    Sleep(999999)
EndFunc

Func Func2()
    Sleep(5000)
EndFunc

;Begin script
Func1() ;Start at this function.
;End script

Sample runthrough of program:

You set $x to 5 and Sleep for 1 second, then you set $y to 10 and while it is sleeping for 1 second, you press "2" to go to Func2 and sleep for 5 seconds. When the 5 seconds is finished, the program should finish (not go back to Func1). The same applies for pressing "1".

A simple If statement can be implemented to jump to the correct function based on what the $currentFunc is set to +/- 1.

Link to comment
Share on other sites

59 minutes ago, IAMK said:

@badcoder123

Sorry, I tried to make it as simple to understand as possible without showing code.

The hotkeys are fine. There is no While. It will execute from func1->func50 and then the script is finished.

A function will not move from func1->func2 until "2" is pressed. (I'm thinking of just putting Sleep(600000) at the end of each function since the user will be operating the script)

Func Func1()
    $x = 5
    Sleep(1000)
    $y = 10
    Sleep(1000)
    $z = 15
    Sleep(999999)
EndFunc

Func Func2()
    Sleep(5000)
EndFunc

;Begin script
Func1() ;Start at this function.
;End script

Sample runthrough of program:

You set $x to 5 and Sleep for 1 second, then you set $y to 10 and while it is sleeping for 1 second, you press "2" to go to Func2 and sleep for 5 seconds. When the 5 seconds is finished, the program should finish (not go back to Func1). The same applies for pressing "1".

A simple If statement can be implemented to jump to the correct function based on what the $currentFunc is set to +/- 1.

Ehhh, still super confused with what you're looking for. Are you trying to do something like this?

HotKeySet("{F1}", "_Terminate")
HotKeySet("1", "Function1")
HotKeySet("2", "Function2")

For $i = 1 To 2                        ;1 To $n functions
   AdlibRegister("Function"&$i,0)      ;Probably a better way to call the function than this...
   Sleep(1000)
Next

Func _Terminate()                       ;Press F1 to end script completly
   Exit
EndFunc

Func Function1()
   MsgBox(0,"","Example function 1.",1) ;Stays open for 1 second but remains active
   AdlibUnRegister("Function1")
EndFunc

Func Function2()
   MsgBox(0,"","Example function 2.",1) ;Stays open for 1 second but remains active
   AdlibUnRegister("Function2")
EndFunc

seems kind of redundant if this is what you're looking for,

Edited by badcoder123
Link to comment
Share on other sites

Just throwing in my interpretation/solution of your problem.

Global $currentFunc = 1

HotKeySet("1", "previous_func")
HotKeySet("2", "next_func")

For $currentFunc = 1 To 10
    Call("func" & $currentFunc)
    Sleep(500)
Next


Func previous_func()
    $currentFunc -= 1
EndFunc

Func next_func()
    $currentFunc += 1
EndFunc


Func func1()
    ConsoleWrite("func1" & @CRLF)
EndFunc

Func func2()
    ConsoleWrite("func2" & @CRLF)
EndFunc

Func func3()
    ConsoleWrite("func3" & @CRLF)
EndFunc

Func func4()
    ConsoleWrite("func4" & @CRLF)
EndFunc

Func func5()
    ConsoleWrite("func5" & @CRLF)
EndFunc

Func func6()
    ConsoleWrite("func6" & @CRLF)
EndFunc

Func func7()
    ConsoleWrite("func7" & @CRLF)
EndFunc

Func func8()
    ConsoleWrite("func8" & @CRLF)
EndFunc

Func func9()
    ConsoleWrite("func9" & @CRLF)
EndFunc

Func func10()
    ConsoleWrite("func10" & @CRLF)
EndFunc

 

Edit:

Quote

and does not continue the function it was in before pressing the hotkey

Nevermind, probably not what you are looking for then.

Edited by Floops
Link to comment
Share on other sites

IAMK,

Some func stuff I've done in the past.  May give you some ideas...

local $func = f1

if IsFunc($func) then
    $func()
else
    ConsoleWrite('not a function' & @LF)
endif

func f1()
    ConsoleWrite('f1' & @LF)
endfunc
local $curr = 0

hotkeyset('{1}','_driver')
hotkeyset('{2}','_driver')
hotkeyset('{3}','_driver')
hotkeyset('{4}','_driver')
hotkeyset('{5}','_driver')

while 1
    sleep(100)
wend

func _1()
    ConsoleWrite('at func 1' & @CRLF)
    sleep(5000)
endfunc
func _2()
    ConsoleWrite('at func 2' & @CRLF)
    sleep(5000)
endfunc
func _3()
    ConsoleWrite('at func 3' & @CRLF)
    sleep(5000)
endfunc
func _4()
    ConsoleWrite('at func 4' & @CRLF)
    sleep(5000)
endfunc
func _5()
    ConsoleWrite('at func 5' & @CRLF)
    sleep(5000)
endfunc

func _driver()

    $curr = '_' & @hotkeypressed
    $curr = stringregexpreplace($curr,'\{(\d*)\}','\1')
    call($curr)

endfunc
#include <date.au3>

Global $curmsg, $prevmsg

;
; Driver loop for function calls.
; Function name must be an underscore followed by whatever code you wish to act on.
;

While 1

    $curmsg = _GetNum()

    If $curmsg = $prevmsg Then ContinueLoop

    Call('_' & $curmsg)
    If @error <> 0xdead Then $prevmsg = $curmsg

    Sleep(100)

WEnd

;
; Function calls for codes to act on
;

Func _0()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_0

Func _1()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_1

Func _5()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_5

Func _9()
    ConsoleWrite('!Firing _' & $curmsg & ' ' & _Now() & @CRLF)
EndFunc   ;==>_9

;
; test function
;

Func _GetNum()
    Return Random(1, 9, 1)
EndFunc   ;==>_GetNum

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

IAMK,

Quote

To me they seem the same, except Call() can't pass a parameter by reference.

Pretty much...there is also an array setup for passing multiple parms with "call".  In general call is not used.  I could not find a way to use the var name as a 1st class object when deriving the name from a string (at least that is why I think it is failing).  That would have been the best solution (see the 1ST example I posted for how it works).  Example #2 of the code I posted is what I think will be most useful to you.  It consists of functions and a driver function that processes the hotkeys.

Good Luck,

kylomas

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

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