PageRenderTime 27ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/AutoHotkey.docset/Contents/Resources/Documents/commands/Input.htm

https://gitlab.com/ahkscript/Autohotkey.docset
HTML | 147 lines | 136 code | 11 blank | 0 comment | 0 complexity | 9a7a3a73b64b4bb3f2d776b9b8d85d82 MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Input</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  7. <link href="../static/theme.css" rel="stylesheet" type="text/css" />
  8. <script src="../static/content.js" type="text/javascript"></script>
  9. </head>
  10. <body>
  11. <h1>Input</h1>
  12. <p>Waits for the user to type a string.</p>
  13. <pre class="Syntax">Input [, OutputVar, Options, EndKeys, MatchList]</pre>
  14. <h3>Parameters</h3>
  15. <dl>
  16. <dt>OutputVar</dt>
  17. <dd><p>The name of the variable in which to store the text entered by the user (by default, artificial input is also captured).</p>
  18. <p>If this and the other parameters are omitted, any Input in progress in another <a href="../misc/Threads.htm">thread</a> is terminated and its <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the word NewInput. By contrast, the <a href="../misc/ErrorLevel.htm">ErrorLevel</a> of the current command will be set to 0 if it terminated a prior Input, or 1 if there was no prior Input to terminate.</p>
  19. <p><em>OutputVar</em> does not store keystrokes per se. Instead, it stores characters produced by keystrokes according to the active window's keyboard layout/language. Consequently, keystrokes that do not produce characters (such as PageUp and Escape) are not stored (though they can be recognized via the <em>EndKeys</em> parameter below).</p>
  20. <p>Whitespace characters such as TAB (`t) are stored literally. ENTER is stored as linefeed (`n).</p></dd>
  21. <dt>Options</dt>
  22. <dd><p><u>A string of zero or more of the following letters (in any order, with optional spaces in between):</u></p>
  23. <p><strong>B</strong>: Backspace is ignored. Normally, pressing backspace during an Input will remove the most recently pressed character from the end of the string. Note: If the input text is visible (such as in an editor) and the arrow keys or other means are used to navigate within it, backspace will still remove the last character rather than the one behind the caret (insertion point).</p>
  24. <p><strong>C</strong>: Case sensitive. Normally, <em>MatchList</em> is not case sensitive (in versions prior to 1.0.43.03, only the letters A-Z are recognized as having varying case, not letters like &uuml;/&Uuml;).</p>
  25. <p><strong>I</strong>: Ignore input generated by any AutoHotkey script, such as the <a href="Send.htm#SendEvent">SendEvent</a> command. However, the <a href="Send.htm#SendInput">SendInput</a> and <a href="Send.htm#SendPlay">SendPlay</a> methods are always ignored, regardless of this setting.</p>
  26. <p><strong>L</strong>: Length limit (e.g. <code>L5</code>). The maximum allowed length of the input. When the text reaches this length, the Input will be terminated and <a href="../misc/ErrorLevel.htm">ErrorLevel</a> will be set to the word Max unless the text matches one of the <em>MatchList</em> phrases, in which case <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the word Match. If unspecified, the length limit is 16383, which is also the absolute maximum.</p>
  27. <p><strong>M</strong>: Modified keystrokes such as Control-A through Control-Z are recognized and transcribed if they correspond to real ASCII characters. Consider this example, which recognizes Control-C:</p>
  28. <pre>Transform, CtrlC, Chr, 3 <em>; Store the character for Ctrl-C in the CtrlC var.</em>
  29. Input, OutputVar, L1 M
  30. if OutputVar = %CtrlC%
  31. MsgBox, You pressed Control-C.
  32. ExitApp</pre>
  33. <p>Note: The characters Ctrl-A through Ctrl-Z correspond to <a href="../Functions.htm#Chr">Chr(1)</a> through <a href="../Functions.htm#Chr">Chr(26)</a>. Also, the <strong>M</strong> option might cause some keyboard shortcuts such as Ctrl-LeftArrow to misbehave while an Input is in progress.</p>
  34. <p><strong>T</strong>: Timeout (e.g. <code>T3</code>). The number of seconds to wait before terminating the Input and setting <a href="../misc/ErrorLevel.htm">ErrorLevel</a> to the word Timeout. If the Input times out, <em>OutputVar</em> will be set to whatever text the user had time to enter. This value can be a floating point number such as 2.5.</p>
  35. <p id="vis"><strong>V</strong>: Visible. Normally, the user's input is blocked (hidden from the system). Use this option to have the user's keystrokes sent to the active window.</p>
  36. <p id="asterisk"><strong>*</strong>: Wildcard (find anywhere). Normally, what the user types must exactly match one of the <em>MatchList</em> phrases for a match to occur. Use this option to find a match more often by searching the entire length of the input text.</p>
  37. <p id="E"><strong>E</strong> <span class="ver">[v1.1.20+]</span>: Handle single-character end keys by character code instead of by keycode. This provides more consistent results if the active window's keyboard layout is different to the script's keyboard layout. It also prevents key combinations which don't actually produce the given end characters from ending input; for example, if <code>@</code> is an end key, on the US layout Shift+2 will trigger it but Ctrl+Shift+2 will not (if the E option is used). If the <strong>C</strong> option is also used, the end character is case-sensitive.</p>
  38. </dd>
  39. <dt>EndKeys</dt>
  40. <dd><p>A list of zero or more keys, any one of which terminates the Input when pressed (the <em>EndKey</em> itself is not written to <em>OutputVar</em>). When an Input is terminated this way, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to the word EndKey followed by a colon and the name of the <em>EndKey</em>. Examples: <code>EndKey:.</code>, <code>EndKey:Escape</code>.</p>
  41. <p>The <em>EndKey</em> list uses a format similar to the <a href="Send.htm">Send</a> command. For example, specifying <code>{Enter}.{Esc}</code> would cause either ENTER, period (.), or ESCAPE to terminate the Input. To use the braces themselves as end keys, specify <code>{{}</code> and/or <code>{}}</code>.</p>
  42. <p>To use Control, Alt, or Shift as end-keys, specify the left and/or right version of the key, not the neutral version. For example, specify <code>{LControl}{RControl}</code> rather than <code>{Control}</code>.</p>
  43. <p>Although modified keys such as Control-C (^c) are not supported, certain characters that require the shift key to be held down -- namely punctuation marks such as ?!:@&amp;{} -- are supported in v1.0.14+. Other characters are supported with the <code>E</code> option described above, in v1.1.20+.</p>
  44. <p>An explicit virtual key code such as <code>{vkFF}</code> may also be specified. This is useful in the rare case where a key has no name and produces no visible character when pressed. Its virtual key code can be determined by following the steps at the bottom fo the <a href="../KeyList.htm#SpecialKeys">key list page</a>.</p>
  45. </dd>
  46. <dt>MatchList</dt>
  47. <dd><p>A comma-separated list of key phrases, any of which will cause the Input to be terminated (in which case <a href="../misc/ErrorLevel.htm">ErrorLevel</a> will be set to the word Match). The entirety of what the user types must exactly match one of the phrases for a match to occur (unless the <a href="#asterisk">* option</a> is present). In addition, <strong>any spaces or tabs around the delimiting commas are significant</strong>, meaning that they are part of the match string. For example, if <em>MatchList</em> is &quot;ABC , XYZ &quot;, the user must type a space after ABC or before XYZ to cause a match.</p>
  48. <p>Two consecutive commas results in a single literal comma. For example, the following would produce a single literal comma at the end of string: &quot;string1,,,string2&quot;. Similarly, the following list contains only a single item with a literal comma inside it: &quot;single,,item&quot;.</p>
  49. <p>Because the items in <em>MatchList</em> are not treated as individual parameters, the list can be contained entirely within a variable. In fact, all or part of it must be contained in a variable if its length exceeds 16383 since that is the maximum length of any script line. For example, <em>MatchList</em> might consist of <code>%List1%,%List2%,%List3%</code> -- where each of the variables contains a large sub-list of match phrases.</p>
  50. </dd>
  51. </dl>
  52. <h3>ErrorLevel</h3>
  53. <p><span class="ver">[v1.1.04+]</span> This command is able to throw an exception if called with no parameters and there is no Input in progress. For more information, see <a href="Catch.htm#RuntimeErrors">Runtime Errors</a>.</p>
  54. <table class="info">
  55. <tr>
  56. <td style="width:15%"><p>1 or 0</p></td>
  57. <td><p>Whenever the command is used without parameters, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> is set to 0 if it successfully terminates a prior input, or 1 if there is no Input in progress.</p></td>
  58. </tr>
  59. <tr>
  60. <td>NewInput</td>
  61. <td>The Input was interrupted by another <a href="../misc/Threads.htm">thread</a> that used the Input command.</td>
  62. </tr>
  63. <tr>
  64. <td>Max</td>
  65. <td>The Input reached the maximum allowed length and it does not match any of the items in <em>MatchList</em>.</td>
  66. </tr>
  67. <tr>
  68. <td>Timeout</td>
  69. <td>The Input timed out.</td>
  70. </tr>
  71. <tr>
  72. <td>Match</td>
  73. <td>The Input matches one of the items in <em>MatchList</em>.</td>
  74. </tr>
  75. <tr>
  76. <td>EndKey:<em>name</em></td>
  77. <td>
  78. <p>One of the <em>EndKeys</em> was pressed to terminate the Input. In this case, <a href="../misc/ErrorLevel.htm">ErrorLevel</a> contains the word EndKey followed by a colon and the name of the terminating key without braces, e.g. &quot;EndKey:Enter&quot;, &quot;EndKey:Escape&quot;, etc.</p>
  79. <p>Note that <em>name</em> is the "normalized" name of the key regardless of how it was written in <em>EndKeys</em>. For example, {Esc} and {vk1B} both produce "ErrorLevel:Escape". <a href="../Functions.htm#GetKeyName">GetKeyName()</a> can be used to retrieve the normalized name.</p>
  80. <p>If the <a href="#E">E option</a> was used, <em>name</em> is the actual character which was typed (if applicable). Otherwise, the key name is determined according to the script's active keyboard layout.</p>
  81. <p>Prior to <span class="ver">[v1.1.20]</span>, if the VK code of the end key was in the range 0x41 (A) through 0x5A (Z), ErrorLevel usually contained the corresponding ASCII character even if that wasn't correct for the current keyboard layout. In v1.1.20 and later, the correct character is used. If a character in the range A through Z is used, it is upper-case for backward-compatibility; otherwise it is usually lower-case.</p>
  82. </td>
  83. </tr>
  84. </table>
  85. <h3>Remarks</h3>
  86. <p>If this command is used while an Input is already in progress in another <a href="../misc/Threads.htm">thread</a>, that Input will be terminated and its <a href="../misc/ErrorLevel.htm">ErrorLevel</a> will be set to the word NewInput. After that (if parameters were given), the new Input will commence.</p>
  87. <p>While an Input is in progress, new <a href="../misc/Threads.htm">threads</a> such as <a href="Menu.htm">custom menu items</a> and <a href="SetTimer.htm">timed subroutines</a> can still be created. Similarly, keyboard <a href="../Hotkeys.htm">hotkeys</a> are still in effect if the Input is <a href="#vis">visible</a>. If the Input is not visible, only <a href="_UseHook.htm">hook hotkeys</a> can be triggered.</p>
  88. <p>When a script first uses this command, the <a href="_InstallKeybdHook.htm">keyboard hook</a> is installed (if it is not already). In addition, the script becomes <a href="_Persistent.htm">persistent</a>, meaning that <a href="ExitApp.htm">ExitApp</a> should be used to terminate it. The keyboard hook will stay installed until the next use of the <a href="Suspend.htm">Suspend</a> or <a href="Hotkey.htm">Hotkey</a> command, at which time it is removed if not required by any hotkeys or hotstrings.</p>
  89. <p>If you use multiple languages or keyboard layouts, Input uses the keyboard layout of the active window rather than the script's (regardless of whether the Input is <a href="#vis">visible</a>). However, in versions prior to 1.0.44.03, the script's own layout is used.</p>
  90. <p>Although not as flexible, <a href="../Hotstrings.htm">hotstrings</a> are generally easier to use than the Input command.</p>
  91. <h3>Related</h3>
  92. <p><a href="KeyWait.htm">KeyWait</a>, <a href="../Hotstrings.htm">Hotstrings</a>, <a href="InputBox.htm">InputBox</a>, <a href="_InstallKeybdHook.htm">#InstallKeybdHook</a>, <a href="../misc/Threads.htm">Threads</a>, <a href="IfIn.htm">if var in/contains MatchList</a></p>
  93. <h3>Examples</h3>
  94. <pre class="NoIndent"><em>; Wait for the user to press any key. Keys that produce no visible character -- such as
  95. ; the modifier keys, function keys, and arrow keys -- are listed as end keys so that they
  96. ; will be detected too.</em>
  97. Input, SingleKey, L1, {LControl}{RControl}{LAlt}{RAlt}{LShift}{RShift}{LWin}{RWin}{AppsKey}{F1}{F2}{F3}{F4}{F5}{F6}{F7}{F8}{F9}{F10}{F11}{F12}{Left}{Right}{Up}{Down}{Home}{End}{PgUp}{PgDn}{Del}{Ins}{BS}{Capslock}{Numlock}{PrintScreen}{Pause}</pre>
  98. <p>&nbsp;</p>
  99. <pre class="NoIndent"><em>; This is a working hotkey example. Since the hotkey has the tilde (~)
  100. ; prefix, its own keystroke will pass through to the active window.
  101. ; Thus, if you type [btw (or one of the other match
  102. ; phrases) in any editor, the script will automatically perform an
  103. ; action of your choice (such as replacing the typed text):</em>
  104. ~[::
  105. Input, UserInput, V T5 L4 C, {enter}.{esc}{tab}, btw,otoh,fl,ahk,ca
  106. if (ErrorLevel = "Max")
  107. {
  108. MsgBox, You entered &quot;%UserInput%&quot;, which is the maximum length of text.
  109. return
  110. }
  111. if (ErrorLevel = "Timeout")
  112. {
  113. MsgBox, You entered &quot;%UserInput%&quot; at which time the input timed out.
  114. return
  115. }
  116. if (ErrorLevel = "NewInput")
  117. return
  118. If InStr(ErrorLevel, "EndKey:")
  119. {
  120. MsgBox, You entered &quot;%UserInput%&quot; and terminated the input with %ErrorLevel%.
  121. return
  122. }
  123. <em>; Otherwise, a match was found.</em>
  124. if (UserInput = "btw")
  125. Send, {backspace 4}by the way
  126. else if (UserInput = "otoh")
  127. Send, {backspace 5}on the other hand
  128. else if (UserInput = "fl")
  129. Send, {backspace 3}Florida
  130. else if (UserInput = "ca")
  131. Send, {backspace 3}California
  132. else if (UserInput = "ahk")
  133. Run, https://autohotkey.com
  134. return</pre>
  135. </body>
  136. </html>