/docs/commands/_If.htm
HTML | 58 lines | 46 code | 12 blank | 0 comment | 0 complexity | eb6299f0b053dc5746e42aea8b2f6615 MD5 | raw file
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <title>#If</title>
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
- <link href="../css/default.css" rel="stylesheet" type="text/css">
- </head>
- <body>
-
-
- <h1>#If <span class="ver">[AHK_L]</span></h1>
-
- <p>创建上下文相关的 <a href="../Hotkeys.htm">热键</a> 和 <a href="../Hotstrings.htm">热字串</a>. 这样的热键根据表达式的结果执行不同的操作 (或什么都不做).</p>
-
- <pre class="Syntax">#If [, Expression ]</pre>
- <h3>参数</h3>
- <table class="info">
- <tr>
- <td>Expression</td>
- <td>任何有效的 <a href="../Variables.htm#Expressions">表达式</a>.</td>
- </tr>
- </table>
-
- <h3>基本操作</h3>
- <p>可以使用任何有效的表达式定义激活活动的上下文. 例如:</p>
- <pre>#If WinActive("ahk_class Notepad") or WinActive(MyWindowTitle)
- #Space::MsgBox You pressed Win+Spacebar in Notepad or %MyWindowTitle%.</pre>
- <p>与 #IfWin 指令一样, #If 是可选的: 它影响脚本中所有实际在它后面的热键和热字串. #If 和 #IfWin 同时也是互斥的; 即只有最近的 #If 或 #IfWin 会起作用.</p>
- <p>要关闭上下文相关性, 请指定 #If 或任一 #IfWin 指令但省略所有参数. 例如:</p>
- <pre>#If</pre>
-
- <h3>一般说明</h3>
- <p>当按下了按键组合的其中一种形式的热键, #If 表达式的计算结果决定了是否应该激活热键. 只有在表达式计算完成或 <a href="_IfTimeout.htm">超时</a> 后系统才会响应键盘输入.</p>
- <p>由于 AutoHotkey 设计上的怪癖, #If 表达式在某些情况下可能被计算两次. 因此, #If 表达式应该设计为没有副作用.</p>
- <p><span class="ver">[AHK_L 53+]:</span> <a href="../Variables.htm#ThisHotkey">A_ThisHotkey</a> 包含了当前正在求值的 #If 表达式的热键名称</p>
-
- <h3>相关</h3>
- <p>大部分 <a href="_IfWinActive.htm">#IfWin</a> 指令的行为属性也适用于 #If.</p>
- <p><a href="_IfTimeout.htm">可以使用 </a>#IfTimeout 覆盖默认的超时时间值.</p>
-
- <h3 id="Examples">示例</h3>
- <pre class="NoIndent">
- <em>; 示例 1: Adjust volume by scrolling the mouse wheel over the taskbar.</em>
- #If MouseIsOver("ahk_class Shell_TrayWnd") WheelUp::Send {Volume_Up} WheelDown::Send {Volume_Down} MouseIsOver(WinTitle) { MouseGetPos,,, Win return WinExist(WinTitle . " ahk_id " . Win) }
-
- <em>; 示例 2: Simple word-delete shortcuts for all Edit controls.</em>
- #If ActiveControlIsOfClass("Edit") ^BS::Send ^+{Left}{Del} ^Del::Send ^+{Right}{Del} ActiveControlIsOfClass(Class) { ControlGetFocus, FocusedControl, A ControlGet, FocusedControlHwnd, Hwnd,, %FocusedControl%, A WinGetClass, FocusedControlClass, ahk_id %FocusedControlHwnd% return (FocusedControlClass=Class) }
-
- <em>; 示例 3: Context-insensitive hotkey.</em>
- #If Esc::ExitApp
-
- <em id="ex4">; 示例 4: Dynamic hotkeys. <b>Requires Example 1.</b></em>
- NumpadAdd:: Hotkey, If, MouseIsOver("ahk_class Shell_TrayWnd") if (doubleup := !doubleup) Hotkey, WheelUp, DoubleUp else Hotkey, WheelUp, WheelUp return DoubleUp: Send {Volume_Up 2} return
- </pre>
-
- </body>
- </html>