PageRenderTime 28ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/AutoHotkey.docset/Contents/Resources/Documents/misc/Remap.htm

https://gitlab.com/ahkscript/Autohotkey.docset
HTML | 181 lines | 175 code | 6 blank | 0 comment | 0 complexity | fcdbe768b13367d30fd0002fa202a912 MD5 | raw file
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>
  4. <title>Remapping Keys and Buttons</title>
  5. <meta name="description" content="Free keyboard remapper that can also remap mouse and joystick buttons. It can also automate repetitive tasks by sending keystrokes &amp; mouse clicks.">
  6. <meta name="keywords" content="keyboard,remapper,remap,remapping,keys,key,keystrokes,clicks,mouse,buttons,button,joystick,hotkeys,hotkey">
  7. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  8. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  9. <link href="../static/theme.css" rel="stylesheet" type="text/css" />
  10. <script src="../static/content.js" type="text/javascript"></script>
  11. </head>
  12. <body>
  13. <h1>Remapping Keys and Buttons</h1>
  14. <h2>Introduction</h2>
  15. <p><strong>Limitation</strong>: AutoHotkey's remapping feature described below is generally not as pure and effective as remapping directly via the Windows registry. For the advantages and disadvantages of each approach, see <a href="#registry">registry remapping</a>.</p>
  16. <h2 id="Remap">Remapping the Keyboard and Mouse</h2>
  17. <p>The syntax for the built-in remapping feature is <code>OriginKey::DestinationKey</code>. For example, a <a href="../Scripts.htm">script</a> consisting only of the following line would make the &quot;a&quot; key behave like the &quot;b&quot; key:</p>
  18. <pre>a::b</pre>
  19. <p>The above example does not alter the &quot;b&quot; key itself. The &quot;b&quot; key would continue to send the &quot;b&quot; keystroke unless you remap it to something else as shown in the following example:</p>
  20. <pre>a::b
  21. b::a</pre>
  22. <p>The examples above use lowercase, which is recommended for most purposes because it also remaps the corresponding uppercase letters (that is, it will send uppercase when Capslock is &quot;on&quot; or the Shift key is held down). By contrast, specifying an uppercase letter on the right side forces uppercase. For example, the following line would produce an uppercase B when you type either &quot;a&quot; or &quot;A&quot; (as long as Capslock is off):</p>
  23. <pre>a::B</pre>
  24. <p>&nbsp;</p>
  25. <p id="RemapMouse"><strong>Mouse remapping</strong>: To remap the mouse instead of the keyboard, use the same approach. For example:</p>
  26. <table class="info">
  27. <tr>
  28. <td>MButton::Shift</td>
  29. <td>Makes the middle button behave like the Shift key.</td>
  30. </tr>
  31. <tr>
  32. <td>XButton1::LButton</td>
  33. <td>Makes the fourth mouse button behave like the left mouse button.</td>
  34. </tr>
  35. <tr>
  36. <td>RAlt::RButton</td>
  37. <td>Makes the right Alt key behave like the right mouse button.</td>
  38. </tr>
  39. </table>
  40. <p>&nbsp;</p>
  41. <p><strong>Other useful remappings:</strong></p>
  42. <table class="info">
  43. <tr>
  44. <td>Capslock::Ctrl</td>
  45. <td>Makes Capslock become a Control key. To retain the ability to turn Capslock on and off, add the remapping <code>+Capslock::Capslock</code> first. This toggles Capslock on and off when you hold down the Shift key and press Capslock. Because both remappings allow additional modifier keys to be held down, the more specific <code>+Capslock::Capslock</code> remapping must be placed first for it to work.</td>
  46. </tr>
  47. <tr>
  48. <td>XButton2::^LButton</td>
  49. <td>Makes the fifth mouse button (XButton2) produce Control-LeftClick.</td>
  50. </tr>
  51. <tr>
  52. <td>RAlt::AppsKey</td>
  53. <td>Makes the right Alt key become the Apps key (which is the key that opens the context menu).</td>
  54. </tr>
  55. <tr>
  56. <td>RCtrl::RWin</td>
  57. <td>Makes the right Control key become the right Windows key.</td>
  58. </tr>
  59. <tr>
  60. <td>Ctrl::Alt</td>
  61. <td>Makes both Control keys behave like an Alt key. However, see <a href="#AltTab">alt-tab issues</a>.</td>
  62. </tr>
  63. <tr>
  64. <td>^x::^c</td>
  65. <td>Makes Control-X produce Control-C. It also makes Control-Alt-X produce Control-Alt-C, etc.</td>
  66. </tr>
  67. <tr>
  68. <td>RWin::Return</td>
  69. <td>Disables the right Windows key by having it simply <a href="../commands/Return.htm">return</a>.</td>
  70. </tr>
  71. </table>
  72. <p>You can try out any of these examples by copying them into a new text file such as &quot;Remap.ahk&quot;, then launching the file.</p>
  73. <p>See the <a href="../KeyList.htm">Key List</a> for a complete list of key and mouse button names.</p>
  74. <h2>Remarks</h2>
  75. <p>The directives <a href="../commands/_IfWinActive.htm">#IfWinActive/Exist</a> can be used to make selected remappings active only in the windows you specify. For example:</p>
  76. <pre>#IfWinActive ahk_class Notepad
  77. a::b <em>; Makes the 'a' key send a 'b' key, but only in Notepad.</em>
  78. #IfWinActive <em>; This puts subsequent remappings and hotkeys in effect for all windows.</em></pre>
  79. <p>Remapping a key or button is &quot;complete&quot; in the following respects:</p>
  80. <ul>
  81. <li>Holding down a modifier such as Control or Shift while typing the origin key will put that modifier into effect for the destination key. For example, <code>b::a</code> would produce Control-A if you press Control-B.</li>
  82. <li>Capslock generally affects remapped keys in the same way as normal keys.</li>
  83. <li>The destination key or button is held down for as long as you continue to hold down the origin key. However, some games do not support remapping; in such cases, the keyboard and mouse will behave as though not remapped.</li>
  84. <li>Remapped keys will auto-repeat while being held down (except keys remapped to become mouse buttons).</li>
  85. </ul>
  86. <p id="HookHotkeys">Although a remapped key can trigger normal hotkeys, by default it cannot trigger mouse hotkeys or <a href="../commands/_UseHook.htm">hook hotkeys</a> (use <a href="../commands/ListHotkeys.htm">ListHotkeys</a> to discover which hotkeys are &quot;hook&quot;). For example, if the remapping <code>a::b</code> is in effect, pressing Ctrl-Alt-A would trigger the <code>^!b</code> hotkey only if <code>^!b</code> is not a hook hotkey. If <code>^!b</code> is a hook hotkey, you can define <code>^!a</code> as a hotkey if you want Ctrl-Alt-A to perform the same action as Ctrl-Alt-B. For example:</p>
  87. <pre>a::b
  88. ^!a::
  89. ^!b::
  90. ToolTip You pressed %A_ThisHotkey%.
  91. return</pre>
  92. <p>Alternatively, in v1.1.06 and later, <a href="../commands/_InputLevel.htm">#InputLevel</a> can be used to override the default behaviour. For example:</p>
  93. <pre>#InputLevel 1
  94. a::b
  95. #InputLevel 0
  96. ^!b::
  97. ToolTip You pressed %A_ThisHotkey%.
  98. return</pre>
  99. <p id="SendPlay">If <a href="../commands/SendMode.htm">SendMode</a> is used in the auto-execute section (top part of the script), it affects all remappings. However, since remapping uses <a href="../commands/Send.htm#blind">Send {Blind}</a> and since the <a href="../commands/SendMode.htm">SendPlay mode</a> does not fully support {Blind}, some remappings might not function properly in SendPlay mode (especially Control, Shift, Alt, and Win). To work around this, avoid SendPlay in auto-execute section when you have remappings; then use the command <a href="../commands/Send.htm#SendPlay">SendPlay</a> vs. Send in other places throughout the script. Alternatively, you could translate your remappings into hotkeys (as described below) that explicitly call SendEvent vs. Send.</p>
  100. <p>When a script is launched, each remapping is translated into a pair of <a href="../Hotkeys.htm">hotkeys</a>. For example, a script containing <code>a::b</code> actually contains the following two hotkeys instead:</p>
  101. <pre>*<strong>a</strong>::
  102. SetKeyDelay -1 <em>; If the destination key is a mouse button, SetMouseDelay is used instead.</em>
  103. Send <a href="../commands/Send.htm#blind">{Blind}</a>{<strong>b</strong> DownTemp} <em>; DownTemp is like Down except that other Send commands in the script won't assume &quot;b&quot; should stay down during their Send.</em>
  104. return
  105. *<strong>a up</strong>::
  106. SetKeyDelay -1 <em>; See note below for why press-duration is not specified with either of these SetKeyDelays.</em>
  107. Send {Blind}{<strong>b</strong> Up}
  108. return</pre>
  109. <p>However, the above hotkeys vary under the following circumstances:</p>
  110. <ol>
  111. <li>When the source key is LCtrl and the destination key is an Alt key, the line <code>Send {Blind}{LAlt DownTemp}</code> is replaced by <code>Send {Blind}<strong>{LCtrl Up}</strong>{LAlt DownTemp}</code>. The same is true if the source is RCtrl, except that <code>{RCtrl up}</code> is used.</li>
  112. <li>When a keyboard key is being remapped to become a mouse button (e.g. <code>RCtrl::RButton</code>), the hotkeys above use SetMouseDelay in place of SetKeyDelay. In addition, the first hotkey above is replaced by the following, which prevents the keyboard's auto-repeat feature from generating repeated mouse clicks:
  113. <pre>*RCtrl::
  114. SetMouseDelay -1
  115. if not GetKeyState(&quot;RButton&quot;) <em>; i.e. the right mouse button isn't down yet.</em>
  116. Send {Blind}{RButton DownTemp}
  117. return</pre>
  118. </li>
  119. </ol>
  120. <p>Note that SetKeyDelay's second parameter (<a href="../commands/SetKeyDelay.htm#dur">press duration</a>) is omitted in the hotkeys above. This is because press-duration does not apply to down-only or up-only events such as <code>{b down}</code> and <code>{b up}</code>. However, it does apply to changes in the state of the Shift/Ctrl/Alt/Win keys, which affects remappings such as <code>a::B</code> or <code>a::^b</code>. Consequently, any press-duration a script puts into effect via its <a href="../Scripts.htm#auto">auto-execute section</a> will apply to all such remappings.</p>
  121. <p>Although a pair of keys cannot be directly remapped to single key (e.g. it's invalid to write <code>a &amp; c::b</code>), this effect can be achieved by explicitly adding the up and down hotkeys from the example higher above: simply replace <code>*a::</code> with <code>a &amp; c::</code>, and replace <code>*a up::</code> with <code>a &amp; c up::</code>.</p>
  122. <p>Since remappings are translated into hotkeys as described above, the <a href="../commands/Suspend.htm">Suspend</a> command affects them. Similarly, the <a href="../commands/Hotkey.htm">Hotkey</a> command can disable or modify a remapping. For example, the following two commands would disable the remapping <code>a::b</code>.</p>
  123. <pre>Hotkey, *a, off
  124. Hotkey, *a up, off</pre>
  125. <p id="AltTab">Alt-tab issues: If you remap a key or mouse button to become an Alt key, that key will probably not be able to alt-tab properly. A possible work-around is to add the hotkey <code>*Tab::Send {Blind}{Tab}</code> -- but be aware that it will likely interfere with using the real Alt key to alt-tab. Therefore, it should be used only when you alt-tab solely by means of remapped keys and/or <a href="../Hotkeys.htm#alttab">alt-tab hotkeys</a>.</p>
  126. <p>In addition to the keys and mouse buttons on the <a href="../KeyList.htm">Key List</a> page, the source key may also be a virtual key (VKnn) or scan code (SCnnn) as described on the <a href="../KeyList.htm#SpecialKeys">special keys</a> page. The same is true for the destination key except that it may optionally specify a scan code after the virtual key. For example, <code>sc01e::vk42sc030</code> is equivalent to <code>a::b</code> on most keyboard layouts.</p>
  127. <p>To disable a key rather than remapping it, make it a hotkey that simply <a href="../commands/Return.htm">returns</a>. For example, <code>F1::return</code> would disable the F1 key.</p>
  128. <p>The following keys are not supported by the built-in remapping method:</p>
  129. <ul>
  130. <li>The mouse wheel (WheelUp/Down/Left/Right).</li>
  131. <li>Pause and Break as destination keys (since they match the names of commands).</li>
  132. <li>Curly braces {} as destination keys. Instead use the <a href="../commands/Send.htm#vk">VK/SC method</a>; e.g. <code>x::+sc01A</code> and <code>y::+sc01B</code>.</li>
  133. <li>A percent sign (%) as a destination key. Instead use the <a href="../commands/Send.htm#vk">VK/SC method</a>.</li>
  134. <li>&quot;Return&quot; as a destination key. Instead use &quot;Enter&quot;.</li>
  135. </ul>
  136. <h2>Moving the Mouse Cursor via the Keyboard</h2>
  137. <p>The keyboard can be used to move the mouse cursor as demonstrated by the fully-featured <a href="../scripts/NumpadMouse.htm">Keyboard-To-Mouse script</a>. Since that script offers smooth cursor movement, acceleration, and other features, it is the recommended approach if you plan to do a lot of mousing with the keyboard. By contrast, the following example is a simpler demonstration:</p>
  138. <pre>*#up::MouseMove, 0, -10, 0, R <em>; Win+UpArrow hotkey =&gt; Move cursor upward</em>
  139. *#Down::MouseMove, 0, 10, 0, R <em>; Win+DownArrow =&gt; Move cursor downward</em>
  140. *#Left::MouseMove, -10, 0, 0, R <em>; Win+LeftArrow =&gt; Move cursor to the left</em>
  141. *#Right::MouseMove, 10, 0, 0, R <em>; Win+RightArrow =&gt; Move cursor to the right</em>
  142. *&lt;#RCtrl:: <em>; LeftWin + RightControl =&gt; Left-click (hold down Control/Shift to Control-Click or Shift-Click).</em>
  143. SendEvent {Blind}{LButton down}
  144. KeyWait RCtrl <em>; Prevents keyboard auto-repeat from repeating the mouse click.</em>
  145. SendEvent {Blind}{LButton up}
  146. return
  147. *&lt;#AppsKey:: <em>; LeftWin + AppsKey =&gt; Right-click</em>
  148. SendEvent {Blind}{RButton down}
  149. KeyWait AppsKey <em>; Prevents keyboard auto-repeat from repeating the mouse click.</em>
  150. SendEvent {Blind}{RButton up}
  151. return</pre>
  152. <h2 id="registry">Remapping via the Registry's &quot;Scancode Map&quot;</h2>
  153. <p><strong>Advantages:</strong></p>
  154. <ul>
  155. <li>Registry remapping is generally more pure and effective than <a href="#Remap">AutoHotkey's remapping</a>. For example, it works in a broader variety of games, it has no known <a href="#AltTab">alt-tab issues</a>, and it is capable of firing AutoHotkey's hook hotkeys (whereas AutoHotkey's remapping requires a <a href="#HookHotkeys">workaround</a>).</li>
  156. <li>If you choose to make the registry entries manually (explained below), absolutely no external software is needed to remap your keyboard. Even if you use <a href="http://webpages.charter.net/krumsick/">KeyTweak</a> to make the registry entries for you, KeyTweak does not need to stay running all the time (unlike AutoHotkey).</li>
  157. </ul>
  158. <p><strong>Disadvantages:</strong></p>
  159. <ul>
  160. <li>Registry remapping is relatively permanent: a reboot is required to undo the changes or put new ones into effect.</li>
  161. <li>Its effect is global: it cannot create remappings specific to a particular user, application, or locale.</li>
  162. <li>It cannot send keystrokes that are modified by Shift, Control, Alt, or AltGr. For example, it cannot remap a lowercase character to an uppercase one.</li>
  163. <li>It supports only the keyboard (AutoHotkey has <a href="#RemapMouse">mouse remapping</a> and some <a href="RemapJoystick.htm">limited joystick remapping</a>).</li>
  164. </ul>
  165. <p><strong>How to Apply Changes to the Registry:</strong> There are at least two methods to remap keys via the registry:</p>
  166. <ol>
  167. <li>Use a program like <a href="http://webpages.charter.net/krumsick/">KeyTweak</a> (freeware) to visually remap your keys. It will change the registry for you.</li>
  168. <li>Remap keys manually by creating a .reg file (plain text) and loading it into the registry. This is demonstrated at <a href="http://www.autohotkey.com/forum/post-56216.html#56216">www.autohotkey.com/forum/post-56216.html#56216</a></li>
  169. </ol>
  170. <h2>Related Topics</h2>
  171. <p><a href="../KeyList.htm#Joystick">List of keys and mouse buttons</a><br>
  172. <a href="../commands/GetKeyState.htm">GetKeyState</a><br>
  173. <a href="RemapJoystick.htm">Remapping a joystick</a></p>
  174. </body>
  175. </html>