Hotkeys heriarcal priority

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Luke10
Posts: 3
Joined: 17 Jan 2024, 10:20

Hotkeys heriarcal priority

10 May 2024, 03:12

Hello guys,

I’m wondering whether it’s possible maybe using Hotkey command, to assign a global hotkey that takes precedence over a context-sensitive hotkey, while still maintaining the integrity of the context structure?

Code: Select all

!^f10:: 
   Hotkey, f10, myLabel 
   return

myLabel:
   msgbox, I’d like this to take precedence while random.exe is active
   return


#IF winactive ("ahk_exe random.exe")
f10::
   msgbox, this will be triggered instead
   return
#IF

[Mod edit: Added [code][/code] tags. Please use them yourself when posting code.]


Thank you.
User avatar
mikeyww
Posts: 27169
Joined: 09 Sep 2014, 18:38

Re: Hotkeys heriarcal priority

10 May 2024, 06:20

Another option is below. This behavior can be replicated with a Hotkey command if needed, but you do not actually need it. You can change the directives to get whatever effect you want.

Code: Select all

#Requires AutoHotkey v1.1.33.11
winTitle := "ahk_exe Notepad.exe"

!^F10::
on := True
SoundBeep 1500
Return

F10::MsgBox 64, Information, 333333333333333333

#If on
F10::MsgBox 48, Warning, Precedence

#If WinActive(winTitle)
F10::MsgBox 64, Information, NOT precedence
#If
From the documentation:
Like the #IfWin directives, #If is positional: it affects all hotkeys and hotstrings physically beneath it in the script. #If and #IfWin are also mutually exclusive; that is, only the most recent #If or #IfWin will be in effect. If more than one variant is eligible to fire, only the one closest to the top of the script will fire. The exception to this is the global variant (the one with no #IfWin criteria): It always has the lowest precedence; therefore, it will fire only if no other variant is eligible (this exception does not apply to hotstrings).
To apply only when the target window is active:

Code: Select all

#Requires AutoHotkey v1.1.33.11
winTitle := "ahk_exe Notepad.exe"

!^F10::
on := True
SoundBeep 1500
Return

F10::MsgBox 64, Information, 333333333333333333

#If WinActive(winTitle)
F10::
If on {
 MsgBox 48, Warning, Precedence
} Else {
 MsgBox 64, Information, NOT precedence
}
Return
#If

Code: Select all

#Requires AutoHotkey v1.1.33.11
winTitle := "ahk_exe Notepad.exe"
on       := False

!^F10::
on := True
SoundBeep 1500
Return

F10::
Switch (WinActive(winTitle) > 0) on {
 Case 11: MsgBox Both
 Case 10: MsgBox Only WinActive
 Default: MsgBox Not WinActive
}
Return
Last edited by mikeyww on 10 May 2024, 06:33, edited 1 time in total.
Rohwedder
Posts: 7713
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Hotkeys heriarcal priority

10 May 2024, 06:28

Hallo,
or:

Code: Select all

!^f10::
   Hotkey, IfWinActive, ahk_exe notepad.exe
   Hotkey, f10, f10, Off
   Hotkey, *f10, myLabel
   return

myLabel:
   msgbox, I’d like this to take precedence while random.exe is active
   return


#IFwinactive, ahk_exe notepad.exe
f10::
   msgbox, this will be triggered instead
   return
#IF

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: busymind and 149 guests