Page 1 of 1

Variable results for single hotkey

Posted: 15 Apr 2021, 11:04
by WeThotUWasAToad
Hello,

How do you construct a script so that a given hotkey will result in variable processes depending on the currently active window?

Below is a basic template I wrote for doing the above. It follows the assumption that it will work down looking for an "IF" criteria to be met and when it is, execute the corresponding Process, at which point the script will end.

Is that correct or will the script continue looking for subsequent IFs? If yes, then how can I modify the script so that it will stop once an IF criteria is met?

Code: Select all

F1::
SetTitleMatchMode, 2
IfWinActive, Screen1
	{
	GoSub, Process1
	}
IfWinActive, Screen2
	{
	GoSub, Process2
	}
IfWinActive, Screen3
	{
	GoSub, Process3
	}
Return
Thanks

Re: Variable results for single hotkey  Topic is solved

Posted: 15 Apr 2021, 11:29
by mikeyww
Your fundamental assumption contrasts with how computer programs generally work (though AHK has some exceptions, too). The code runs, line by line, until a command tells it to stop, an error occurs, or the script exits. To end the routine, you can insert additional Return at any point, such as inside any of the braces.

Alternatives are Else, or using an #IfWinActive directive to separate the routines.

Switch is another way to accomplish what you have described: a maximum of one Switch branch will be taken. See the documentation.

Re: Variable results for single hotkey

Posted: 15 Apr 2021, 18:21
by WeThotUWasAToad
Thanks for the comments Mike.

By simply adding the word Else to the beginning of the IF lines, I can get the behavior I want (ie if it finds the defined criteria then it executes the corresponding action and the script stops. Otherwise, the script continues.)

Thanks again

Re: Variable results for single hotkey

Posted: 15 Apr 2021, 19:15
by mikeyww
Good to hear. Alotof luck to you.