AutoHotkey v2 中关闭 #HotIf 和 return 有什么区别?

问题描述 投票:0回答:1

基本上,这有什么区别

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

; Other code that follows

还有这个?

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

; Other code that follows

另外,我是否需要关闭#HotIf(或返回)?

如果我写这个怎么办?

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

; Other code that follows
autohotkey
1个回答
0
投票

Return
在这里不执行任何操作。
在 v1 中,您曾经用
Return
结束热键块,如下所示:

a::
    MsgBox
Return

(想,这不适用于单行热键,就像在您的示例中一样。它们总是只执行一行)

但是在 v2 中,热键块包含在

{ }
中,因此这里不使用
Return

a::
{
    MsgBox()
}

好的,那么进入下一个问题,为什么需要

#HotIf

好吧,它结束了上下文相关的块。如果你永远不会结束你的上下文敏感块,它就永远不会结束。

考虑这个

#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")

你应该做的是:

#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")
© www.soinside.com 2019 - 2024. All rights reserved.