What is the difference between closing #HotIf and return in AutoHotkey v2?

30 Views Asked by At

Basically, what is the difference between this

#HotIf MouseIsOver("ahk_class Shell_TrayWnd")
  WheelUp::Volume_Up
  WheelDown::Volume_Down
#HotIf

; Other code that follows

and this?

#HotIf MouseIsOver("ahk_class Shell_TrayWnd")
  WheelUp::Volume_Up
  WheelDown::Volume_Down
Return

; Other code that follows

Also, do I even need closing #HotIf (or return)?

What if I write this?

#HotIf MouseIsOver("ahk_class Shell_TrayWnd")
  WheelUp::Volume_Up
  WheelDown::Volume_Down

; Other code that follows
1

There are 1 best solutions below

0
0x464e On BEST ANSWER

Return doesn't do anything here.
In v1 you used to end a hotkey block with Return, like so:

a::
    MsgBox
Return

(Thought, this does not apply for one-liner hotkeys, like in your example. They always just execute the one single line)

But in v2, hotkey blocks are enclosed in { }, so Return isn't used here:

a::
{
    MsgBox()
}

Ok, then onto the next question, why do you need #HotIf?
Well it ends the context sensitive block. If you never end your context sensitive block, it never ends.

Consider this

#HotIf WinActive("ahk_exe notepad.exe")
a::MsgBox("notepad specific hotkey 1")
b::MsgBox("notepad specific hotkey 2")

;something else 
;something else

MyCoolFunction()
{
    ;somethingthing
}

MyCoolFunction2()
{
    ;somethingthing
}

c::MsgBox("Oops, I forgot to end the context sensitive hotkey block, and now this hotkey can only be used in notepad")

What you should've done, is:

#HotIf WinActive("ahk_exe notepad.exe")
a::MsgBox("notepad specific hotkey 1")
b::MsgBox("notepad specific hotkey 2")
#HotIf

; something else 
; something else

MyCoolFunction()
{
    ;somethingthing
}

MyCoolFunction2()
{
    ;somethingthing
}

c::MsgBox("Now I can be used outside of notepad, as intended")