Page 1 of 1

Program Specific Hot Key

Posted: 30 May 2023, 14:16
by CuriousDad
Hello.

I wanted to use the F1 key for a hot key function when Firefox was active. However, if the Firefox was not active, I would like it to be the Help function as it normally is in Windows. I wrote the following:

Code: Select all

F1:: ; Cycle Bitwarden Login
	If WinActive("ahk_exe firefox.exe")
		Send "Command"
		return
	If not WinActive("ahk_exe firefox.exe")
		return
return
I know that the not WinActive section is incorrect because I'm only telling it to stop at that point. So I was wondering if there was an instruction that would resume normal key function if Firefox was not active. Thanks.

Re: Program Specific Hot Key

Posted: 30 May 2023, 14:35
by RussF

Code: Select all

#Requires AutoHotkey v1.1.33+

#IfWinActive ahk_exe firefox.exe
	f1::
		Send % "Command"
	Return
#IfWinActive

Code: Select all

#Requires AutoHotkey v2.0+

#HotIf WinActive("ahk_exe firefox.exe")
	f1::
	{
		Send("Command")
	}
#HotIf
No need for an "else" or "if not" clause.

Russ

Re: Program Specific Hot Key

Posted: 30 May 2023, 15:46
by CuriousDad
Oh. That makes sense. Thanks.

Why is the second #IfWinExist needed? Is it to close the thread, but allow the script to continue?

Re: Program Specific Hot Key

Posted: 31 May 2023, 05:26
by RussF
From the #IfWinActive docs:
The #IfWin directives are positional: they affect all hotkeys and hotstrings physically beneath them in the script. They are also mutually exclusive; that is, only the most recent one will be in effect.

To turn off context sensitivity, specify any #IfWin directive but omit all of its parameters. For example:

#IfWinActive
You can have the same hotkey (or group of hotkeys) perform different functions for different applications by listing them under different #IfwinActive directives. The last blank one simply closes the selective directives and means that any more hotkeys defined below that will apply to all applications or conditions. The same is true for #HotIf in version 2. #HotIf is more comprehensive because you can use any logical expression following it to define when the respective hotkeys under it are active. #HotIf in v.2 is the same as #If in v.1.

Code: Select all

#Requires AutoHotkey v1.1.33+

#IfWinActive ahk_exe firefox.exe
	f1::
		MsgBox, % "I am in Firefox"
	Return
#IfWinActive ahk_exe chrome.exe
	f1::
		MsgBox, % "I am in Chrome"
	Return
#IfWinActive ahk_exe Brave.exe
	f1::
		MsgBox, % "I am in Brave"
	Return
#IfWinActive

	f1::
		MsgBox, % "No browser is active"
	Return
Russ

Re: Program Specific Hot Key

Posted: 31 May 2023, 21:41
by CuriousDad
Thanks. One further question.

If I wish for two separate programs to share the same function, how do I go about writing for that?

Currently, I would like to have F1 go to the previous tab for both Windows Explorer and Edge. Both incorporate the same shortcut on Windows. I have the following:

Code: Select all

#IfWinActive ahk_exe explorer.exe or ahk_exe msedge.exe
	F1:: ; Previous Tab
		Send ^+{Tab}
	return
#IfWinActive
It works for Edge, but not for Windows Explorer. I would like the F1 key to be assigned that specific task when either Windows Explorer or Edge is active (not simply exist). Do I need to create a command for each program saying the same thing or can I use a unifying commanding (I thought or might work, but I guess it doesn't).

Re: Program Specific Hot Key

Posted: 31 May 2023, 22:55
by boiler

Code: Select all

#If WinActive("ahk_exe explorer.exe") or WinActive("ahk_exe msedge.exe")
F1::Send ^+{Tab} ; Previous Tab

Re: Program Specific Hot Key

Posted: 01 Jun 2023, 12:17
by CuriousDad
Thanks.

I noticed that this code doesn't utilize #IfWinActive, but rather #If WinActive.

The former required a closing command #IfWinActive, but the other did not. Does the latter require such a closing command?

The former did not require quotation and parentheses while the latter did. Both do the same function. Is there any difference besides the number of lines?

Re: Program Specific Hot Key

Posted: 01 Jun 2023, 12:28
by boiler
CuriousDad wrote: I noticed that this code doesn't utilize #IfWinActive, but rather #If WinActive.
That’s because #If is a specific directive whose condition is defined by an expression, which is what is necessary to use operators like OR, and therefore the function WinActive() is used as part of the expression.

CuriousDad wrote: The former required a closing command #IfWinActive, but the other did not. Does the latter require such a closing command?
It did not require it. It is never required for either one. Don’t look at it as a closing directive. It’s the start of the removal of all conditions for any hotkeys, hotstrings, and remaps that may follow it, assuming you don’t want any conditions on them. If nothing follows, there is no need for it.

CuriousDad wrote: The former did not require quotation and parentheses while the latter did. Both do the same function. Is there any difference besides the number of lines?
The former is a specific directive specifying that the condition is that a window matching those criteria must be active. The latter is a directive where the condition is defined by the expression that follows. Functions calls can be part of an expression, and WinActive() is a function.

Re: Program Specific Hot Key

Posted: 01 Jun 2023, 12:32
by RussF
As in other languages, AHK offers multiple ways to do some things.

Notice that I posted two versions - one for AHK ver. 1 and one for ver. 2.

In version 1, you can user either the command syntax as I did, or the expression syntax as @boiler did. Neither one require a closing #If or #IfWinActive, however, any hotkeys defined after the last #If/#IfWinactive statement will only execute when the stated conditions are true. If you want to define further hotkeys that apply to all conditions, you must use a closing directive.

The same is true in version 2, however, everything is based on expressions and the universal #HotiIf is used.

Russ

Edit: ...and what @boiler said. :D

Re: Program Specific Hot Key

Posted: 02 Jun 2023, 11:53
by CuriousDad
Thanks to both of you for your explanations.