文脈依存のホットキーと ホットストリングを作成します。They perform a different action (or none at all) depending on any condition (an expression).
#HotIf Expression
型:論理値
If omitted, subsequently defined hotkeys/hotstrings are not context-sensitive. Otherwise, specify any valid expression. これは、1つのパラメータ(ThisHotkey)を持つ暗黙の関数の戻り値になります。この関数はグローバル変数を直接変更することはできませんが(通常通り想定ローカルであり、宣言を含むことができないため)、グローバル変数を変更する他の関数を呼び出すことができます。
The #HotIf directive sets the expression which will be used by subsequently defined hotkeys and hotstrings to determine whether they should activate. This expression is evaluated when the hotkey's key, mouse button or combination is pressed, when the hotstring's abbreviation is typed, or at other times when the program needs to know whether the hotkey/hotstring is active.
To make context-sensitive hotkeys/hotstrings, simply precede them with the #HotIf directive. 事例:
#HotIf WinActive("ahk_class Notepad") or WinActive(MyWindowTitle)
#Space::MsgBox "You pressed Win+Spacebar in Notepad or " MyWindowTitle
:X:btw::MsgBox "You typed btw in Notepad or " MyWindowTitle
The #HotIf directive is positional: it affects all hotkeys/hotstrings physically beneath it in the script, until the next #HotIf directive.
注: if文とは異なり、中括弧は#HotIf指示文では効果がありません。
The #HotIf directive only affects hotkeys/hotstrings defined via the double-colon syntax, such as #Space:: or ::btw::. For hotkeys/hotstrings created via the Hotkey or Hotstring function, use the HotIf function.
文脈依存性をオフにするには、#HotIfを無表現で指定します。事例:
#HotIf
他のディレクティブと同様に、#HotIfは条件付きで実行することはできません。
マウスやキーボードのホットキーが#HotIfによって無効化されると、本来の機能を発揮します。つまり、そのようなホットキーがないかのように、アクティブウィンドウに通過します。There is one exception: Controller hotkeys: although #HotIf works, it never prevents other programs from seeing the press of a button.
#HotIfは、Enterや Spaceなどの通常のキーの動作を変更するためにも使用できます。これは、特定のウィンドウがそのキーを無視したり、好ましくない動作をする場合に有効です。事例:
#HotIf WinActive("Reminders ahk_class #32770") ; Outlookの「リマインダー」ウィンドウ。
Enter::Send "!o" ; Enterキーを押すと、選択したリマインダーをスヌーズするのではなく、開くようにします。
#HotIf
特定のホットキーや ホットストリングは、それぞれの定義が異なるHotIf基準であれば、スクリプト内で複数回定義することができます。These are known as hotkey variants or hotstring variants. 事例:
#HotIf WinActive("ahk_class Notepad")
^!c::MsgBox "You pressed Control+Alt+C in Notepad."
#HotIf WinActive("ahk_class WordPadClass")
^!c::MsgBox "You pressed Control+Alt+C in WordPad."
#HotIf
^!c::MsgBox "You pressed Control+Alt+C in a window other than Notepad/WordPad."
複数のバリアントが発射対象となる場合、スクリプトの先頭に最も近いものだけが発射されます。ただし、グローバルバリアント(HotIfの基準がないもの)については例外です:常に最も低い優先順位を持つので、他の variant が適格でない場合にのみ発火します(この例外はホットストリングには適用されません)。
重複するホットキーを作成する場合、^!+#などの修飾記号の順番は関係ありません。例えば、^!cは!^cと同じです。ただし、キーは一貫したスペルで表記する必要があります。例えば、Escはこの目的ではEscapeと同じではありません(ただし、ケースは重要ではありません)。また、ワイルドカード接頭辞(*)を持つホットキーは、ワイルドカードでないものと全く別物です。例えば、*F1とF1は、それぞれ独自のバリアントセットを持っているでしょう。
A window group can be used to make a hotkey/hotstring execute for a group of windows. 事例:
GroupAdd "MyGroup", "ahk_class Notepad"
GroupAdd "MyGroup", "ahk_class WordPadClass"
#HotIf WinActive("ahk_group MyGroup")
#z::MsgBox "You pressed Win+Z in either Notepad or WordPad."
To create hotkey/hotstring variants dynamically (while the script is running), see HotIf.
When the hotkey's key, mouse or controller button combination is pressed or the hotstring's abbreviation is typed, the #HotIf expression is evaluated to determine if the hotkey/hotstring should activate.
Note: Scripts should not assume that the expression is only evaluated when the hotkey's key is pressed (see below).
この式は、プログラムがホットキーが有効かどうかを知る必要があるときに評価されることもあります。例えば、a & b:: のようなカスタムの組み合わせの #HotIf 式は、プレフィックスキー(この例ではa)が押されたときに評価され、カスタム修飾キーとして機能するかどうかを判断することができます。
Note: Use of #HotIf in an unresponsive script may cause input lag or break hotkeys/hotstrings (see below).
#HotIf指令には、さらにいくつかの注意事項があります:
ThisHotkey, A_ThisHotkey and A_TimeSinceThisHotkey are set based on the hotkey or non-auto-replace hotstring for which the current #HotIf expression is being evaluated.
A_PriorHotkey、A_TimeSincePriorHotkeyは、対応する「This」変数の以前の値を一時的に保持します。
WinActiveや WinExistの単純な呼び出しに対して、#HotIfは式の評価を避けるように最適化されており、そのような場合にラグなどの問題が発生するリスクを低減することができます。具体的:
notや !で結果を反転させることができるが、それ以外の演算子は使用できない。これらの条件を満たす式は、プログラムによって直接評価され、ListLinesには表示されません。
Before the Hotkey or Hotstring function is used to modify an existing hotkey/hotstring variant, typically the HotIf function must be used with the original expression text. ただし、所定の条件の組み合わせを持つ最初のユニークな表現は、その条件によって参照することも可能です。事例:
HotIfWinExist "ahk_class Notepad"
Hotkey "#n", "Off" ; ホットキーをオフにします。
HotIf 'WinExist("ahk_class Notepad")'
Hotkey "#n", "On" ; 同じホットキーを再びオンにします。
#HotIf WinExist("ahk_class Notepad")
#n::WinActivate
なお、変数を使用した場合は、表現として失格となる。If the variable's value never changes after the hotkey/hotstring is created, there are two strategies for minimizing the risk of lag or other issues inherent to #HotIf:
HotIfWin...(MyTitleVar) to set the criteria and Hotkey(KeyName, Action) or Hotstring(String, Replacement) to create the hotkey/hotstring variant.#HotIf WinActive("ahk_group MyGroup")のようにし、スクリプト内の別の場所でGroupAdd "MyGroup", MyTitleVarでウィンドウグループを定義します。#HotIf also restores prefix keys to their native function when appropriate (a prefix key is A in a hotkey such as a & b::). この現象は、指定したプリフィクスに対して有効なホットキーがない場合に発生します。
ホットキーが#HotIfによって現在無効になっている場合、そのキーまたはマウスボタンは、KeyHistoryの「Type」列に「#」文字で表示されます。This can help debug a script.
Alt-tabのホットキーは、#HotIfの影響を受けません:これらはすべてのウィンドウで有効です。
Last Found Windowは、#HotIfで設定することができます。事例:
#HotIf WinExist("ahk_class Notepad")
#n::WinActivate ; WinExist()で見つかったウィンドウをアクティブにします。
#HotIfTimeoutを使用すると、デフォルトのタイムアウト値をオーバーライドすることができます。
Hotkey function, Hotkeys, Hotstring function, Hotstrings, Suspend, WinActive, WinExist, SetTitleMatchMode, DetectHiddenWindows
メモ帳がアクティブなときにのみ機能する2つのホットキーと1つのホットストリング、およびメモ帳以外のウィンドウで機能する1つのホットキーを作成します。
#HotIf WinActive("ahk_class Notepad")
^!a::MsgBox "You pressed Ctrl-Alt-A while Notepad is active."
#c::MsgBox "You pressed Win-C while Notepad is active."
::btw::This replacement text for "btw" will occur only in Notepad.
#HotIf
#c::MsgBox "You pressed Win-C in a window other than Notepad."
タスクバー上でマウスホイールをスクロールして音量を調整できるようにします。
#HotIf MouseIsOver("ahk_class Shell_TrayWnd")
WheelUp::Send "{Volume_Up}"
WheelDown::Send "{Volume_Down}"
MouseIsOver(WinTitle) {
MouseGetPos ,, &Win
return WinExist(WinTitle " ahk_id " Win)
}
すべてのEditコントロールに、シンプルな単語削除ショートカットを搭載。
#HotIf ActiveControlIsOfClass("Edit")
^BS::Send "^+{Left}{Del}"
^Del::Send "^+{Right}{Del}"
ActiveControlIsOfClass(Cls) {
FocusedControl := 0
try FocusedControl := ControlGetFocus("A")
FocusedControlClass := ""
try FocusedControlClass := WinGetClass(FocusedControl)
return (FocusedControlClass=Cls)
}
Dynamic Hotkeys. この例は、実行する前に例#2との組み合わせが必要です。
NumpadAdd::
{
static toggle := false
HotIf 'MouseIsOver("ahk_class Shell_TrayWnd")'
if (toggle := !toggle)
Hotkey "WheelUp", DoubleUp
else
Hotkey "WheelUp", "WheelUp"
return
; ネストされた機能:
DoubleUp(ThisHotkey) => Send("{Volume_Up 2}")
}