Vim Tips Wiki
Register
(rework, rough merge in from 353 by Jacques Petit)
(39 intermediate revisions by 23 users not shown)
Line 11: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
This tip shows some methods for mapping keys within the operating system to make life easier in Vim. In particular, it can be convenient to use the CapsLock key for Escape so you don't have to reach for the Esc key.
+
This tip shows some methods for mapping keys within the operating system to make life easier in Vim. In particular, it can be convenient to use the CapsLock key for Escape so you don't have to reach for the Esc key. It is also useful to use the [[#Use the Caps Lock key as Ctrl|CapsLock key for Ctrl]] and press Ctrl-[ instead of Esc.
   
 
This tip is for Windows systems (not Windows 9x). For Unix-based systems, see [[VimTip166|Map caps lock to escape in XWindows]].
 
This tip is for Windows systems (not Windows 9x). For Unix-based systems, see [[VimTip166|Map caps lock to escape in XWindows]].
  +
  +
Some of the procedures here change the way keys behave for all applications. It may be better to use other methods to [[VimTip285|avoid the escape key]] in Vim.
   
 
==Registry==
 
==Registry==
  +
{{RegistryWarning}}
Before trying the following, you should run <tt>regedit.exe</tt> and inspect the relevant registry keys, and export them for a backup. Make sure you know how to remove the registry changes if they cause trouble.
 
  +
  +
Before trying the following, you should run <code>regedit.exe</code> and inspect the relevant registry keys, and export them for a backup. Make sure you know how to remove the registry changes if they cause trouble.
   
For the current user: To map CapsLock to Escape, and ScrollLock to CapsLock, create file (for example) <tt>keys.reg</tt> containing:
+
For the current user (doesn't work in Windows 7 or 8): To map CapsLock to Escape, and ScrollLock to CapsLock, create file (for example) <code>keys.reg</code> containing:
 
<pre>
 
<pre>
 
REGEDIT4
 
REGEDIT4
Line 27: Line 31:
 
Use Explorer to double-click the file to import the value to the registry. To apply the changes, log off and log on. Then, pressing key Esc will generate Escape as normal, pressing CapsLock will also generate Escape, and pressing ScrollLock will generate CapsLock (and you cannot generate ScrollLock).
 
Use Explorer to double-click the file to import the value to the registry. To apply the changes, log off and log on. Then, pressing key Esc will generate Escape as normal, pressing CapsLock will also generate Escape, and pressing ScrollLock will generate CapsLock (and you cannot generate ScrollLock).
   
Alternatively, for the change to apply to all users, run the following reg file and reboot:
+
Alternatively, for the change to apply to all users, run the following reg file and reboot (this key still works in Windows 7 and 8):
 
<pre>
 
<pre>
 
REGEDIT4
 
REGEDIT4
Line 34: Line 38:
 
</pre>
 
</pre>
   
To restore the keyboard, run <tt>regedit.exe</tt> and delete the "Scancode Map" entry from <tt>HKEY_CURRENT_USER</tt> (and log off/on), or from <tt>HKEY_LOCAL_MACHINE</tt> (and reboot), depending on which section of the registry you changed.
+
To restore the keyboard, run <code>regedit.exe</code> and delete the "Scancode Map" entry from <code>HKEY_CURRENT_USER</code> (and log off/on), or from <code>HKEY_LOCAL_MACHINE</code> (and reboot), depending on which section of the registry you changed.
   
  +
==Use the Caps Lock key as Ctrl==
==See also==
 
  +
Particularly while touch typing, it can be useful to remap the keyboard so that pressing CapsLock is the same as pressing Ctrl. This change makes it easy to press Ctrl-[ as an [[VimTip285|alternative to pressing Esc]].
*[[VimTip285|Avoid the escape key]] for Vim methods to make pressing Esc easier
 
   
  +
The following applies to the current user (log off and log on to apply). Alternatively, an administrator can apply the change to all users (reboot to apply) by replacing <code>[HKEY_CURRENT_USER\Keyboard Layout]</code> with <code>[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]</code>
=Comments=
 
Windows:
 
The <tt>Ctrl2cap</tt> program from http://www.sysinternals.com/ is a kernel-mode driver to perform keyboard input filtering to turn CapsLock into LeftCtrl.
 
   
  +
Map CapsLock to LeftCtrl (press CapsLock to generate LeftCtrl):
Linux/Unix:
 
Here's how to map the caps lock key to a control under X windows, at least under xfree86-3.x and 4.x:
 
Add a file named ".Xkbmap" in your home directory with this content:
 
 
<pre>
 
<pre>
  +
REGEDIT4
-option ctrl:nocaps
 
  +
[HKEY_CURRENT_USER\Keyboard Layout]
  +
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00
 
</pre>
 
</pre>
   
  +
An alternative would be to replace the "Scancode Map" line with the following to swap CapsLock and LeftCtrl:
----
 
  +
<pre>
remapkey.exe ----a gui built in to Windows XP and other version that creates needed registry entries to remap keys.
 
  +
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,1d,00,1d,00,3a,00,00,00,00,00
  +
</pre>
   
  +
==Utilities==
----
 
  +
As an alternative to changing the registry, there are various utilities that can remap keys. Many of these can be run by a non-administrator, and can apply mappings only when wanted (for example, when running Vim). They do not require log off/on or reboot to apply changes, unless specified otherwise.
For Windows: I use the AutoHotkey script similar to [[VimTip285]], which does not require administrator rights.
 
   
  +
===KeyTweak===
You can turn off CapsLock when Esc is hit. Use AutoHotkey with script:
 
  +
This tool is a free keyboard remapper for Windows NT/2000/XP/Vista/Win 7. It automates the registry trick above (it makes use of Microsoft's Scancode Map registry key) to remap your keyboard. This app requires you to reboot before changes take effect.
  +
  +
http://webpages.charter.net/krumsick/ (dead link)
  +
  +
http://www.softpedia.com/get/System/OS-Enhancements/KeyTweak.shtml
  +
  +
===SharpKeys===
  +
This tool is a free keyboard mapper, but requires rebooting the machine for the key mapping to take effect. The interface is fairly simple to use, and appears to work on Windows NT/2003 server/2000/XP/Vista/Win 7. By automating the keyboard mapping, this reduces the risk of the user accidentally changing something in the registry.
  +
  +
http://www.randyrants.com/2008/12/sharpkeys_30.html
  +
  +
===AutoHotkey===
  +
[http://www.autohotkey.com/ AutoHotkey] is an open source project to automate sending keys and mouse clicks. It can remap keys, and provides a powerful scripting language, and does not require administrator rights. One of the features of this program is that the key mappings are not permanent, but only last as long as the program is running.
  +
  +
To make Capslock work like Escape, add the following to the default script:
  +
<pre>
  +
Capslock::Esc
  +
</pre>
  +
  +
The following AutoHotkey script will turn off CapsLock and generate Escape when Esc is pressed:
 
<pre>
 
<pre>
 
Esc::
 
Esc::
Line 63: Line 87:
 
Send, {ESC}
 
Send, {ESC}
 
Suspend Off
 
Suspend Off
  +
return
 
</pre>
 
</pre>
   
  +
The following AutoHotkey script maps CapsLock to Ctrl in PuTTY. In other applications, CapsLock works normally.
This will turn off caps and execute an "ESC" whenever Escape is hit.
 
  +
<pre>
  +
classname = ""
  +
keystate = ""
   
  +
*CapsLock::
----
 
  +
WinGetClass, classname, A
Just map an shortcut like 'jj' to send ESC:
 
  +
if (classname = "PuTTY")
imap jj <Esc> "press jj two-times-quickly to ESC to normal mode
 
  +
send,{Ctrl down}
  +
else
  +
GetKeyState, keystate, CapsLock, T
  +
if (keystate = "D")
  +
SetCapsLockState, Off
  +
else
  +
SetCapsLockState, On
  +
return
   
  +
*CapsLock up::
----
 
  +
WinGetClass, classname, A
=Material from tip 353 to be merged in here=
 
  +
if (classname = "PuTTY")
On Windows systems, you can edit the registry to change the code produced when a key is pressed. This tip shows how to change the keyboard layout so that the CapsLock key acts as a Ctrl key.
 
  +
send,{Ctrl up}
  +
return
  +
</pre>
   
  +
The following AutoHotkey script maps CapsLock to ESC in Vim. In other applications, CapsLock works normally.
==Replacing CapsLock with Ctrl==
 
Place the following text into a text file with the extension *.reg and update your registry by double-clicking the file:
 
 
<pre>
 
<pre>
  +
classname = ""
REGEDIT4
 
  +
keystate = ""
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
 
  +
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00
 
  +
*Capslock::
  +
WinGetClass, classname, A
  +
if (classname = "Vim")
  +
{
  +
SetCapsLockState, Off
  +
Send, {ESC}
  +
}
  +
else
  +
{
  +
GetKeyState, keystate, CapsLock, T
  +
if (keystate = "D")
  +
SetCapsLockState, Off
  +
else
  +
SetCapsLockState, On
  +
return
  +
}
  +
return
 
</pre>
 
</pre>
   
  +
The following AutoHotKey script switches CapsLock and Escape. When the application is shut down, the keys return to their normal behavior:
You will probably need to reboot your machine for this system change to take effect.
 
 
====Undoing the change====
 
The verified way to reverse this setting is to delete the new scan codes key that was added.
 
 
(tested this on one of my machines)
 
 
====Swapping keys rather than replacing CapsLock====
 
The registry entry above sets CapsLock to Ctrl. The following registry entry swaps the CapsLock and Ctrl keys on your keyboard. Useful if you sometimes use CapsLock.
 
 
<pre>
 
<pre>
  +
CapsOn=false
REGEDIT4
 
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
 
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,1d,00,3a,00,3a,00,1d,00,00,00,00,00
 
</pre>
 
   
  +
Capslock::
====Setting the key layout for the current user====
 
  +
;suspend to prevent calling esc
If the machine has multiple users who might not like the new layout, instead of using [HKEY_LOCAL_MACHINE\....] you can specify [HKEY_CURRENT_USER\Keyboard Layout]. This will make the changes for one user account only. Additionally, simply logging off and back on will apply the changes (no need to reboot).
 
  +
Suspend on
  +
Send, {ESC}
  +
Suspend off
  +
return
   
  +
Esc::
===Comments===
 
  +
;use global variable to keep track of state
Some explanation is [http://www.annoyances.org/exec/forum/winxp/t1004806455 here].
 
  +
if CapsOn = false
  +
{
  +
CapsOn = true
  +
SetCapsLockState, on
  +
}
  +
else
  +
{
  +
CapsOn = false
  +
SetCapsLockState, off
  +
}
  +
return
   
A more detailed explanation and reference can also be found [http://www.usnetizen.com/fix_capslock.php here].
 
----
 
 
=Will put following on talk page=
 
==Registry==
 
Following are some examples of settings to map the keyboard in different ways.
 
 
Map CapsLock as Escape (press CapsLock to generate Escape):
 
<pre>
 
REGEDIT4
 
[HKEY_CURRENT_USER\Keyboard Layout]
 
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00
 
 
</pre>
 
</pre>
   
  +
The following AutoHotKey script tries to emulate an idea I read about from Steve Losh where he overloaded his "Caps Lock" key to do the following: when pressing it and another key the "Caps Lock" key is equivalent to a "Control", but when just pressing and releasing the "Caps Lock" quickly it sends an "Escape". I believe that this is the best of the both worlds! In his case, since he uses a Macbook, he was using the software "KeyRemap4Macbook", but the equivalent doesn't exist for Windows (that I know of). However, AutoHotkey can emulate this functionality.
The above applies to the current user (log off and log on to apply). Alternatively, an administrator can apply the change to all users (reboot to apply) by replacing <tt>[HKEY_CURRENT_USER\Keyboard Layout]</tt> with <tt>[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]</tt>
 
   
Swap CapsLock and Esc (press Capslock to generate Escape; press Esc to generate CapsLock):
 
 
<pre>
 
<pre>
  +
; Sends Esc if capslock is pressed alone
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,01,00,3a,00,3a,00,01,00,00,00,00,00
 
  +
; Sends Ctrl+key if capslock is pressed with another key
</pre>
 
  +
; TODO: work with arrow keys and other modifiers (e.g. shift, control)
   
  +
SetCapsLockState AlwaysOff
Map CapsLock to LeftCtrl (press CapsLock to generate LeftCtrl):
 
  +
CapsLock::Send {esc}
<pre>
 
  +
CapsLock & a::Send ^a
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00
 
  +
CapsLock & b::Send ^b
  +
CapsLock & c::Send ^c
  +
CapsLock & d::Send ^d
  +
CapsLock & e::Send ^e
  +
CapsLock & f::Send ^f
  +
CapsLock & g::Send ^g
  +
CapsLock & h::Send ^h
  +
CapsLock & i::Send ^i
  +
CapsLock & j::Send ^j
  +
CapsLock & k::Send ^k
  +
CapsLock & l::Send ^l
  +
CapsLock & m::Send ^m
  +
CapsLock & n::Send ^n
  +
CapsLock & o::Send ^o
  +
CapsLock & p::Send ^p
  +
CapsLock & q::Send ^q
  +
CapsLock & r::Send ^r
  +
CapsLock & s::Send ^s
  +
CapsLock & t::Send ^t
  +
CapsLock & u::Send ^u
  +
CapsLock & v::Send ^v
  +
CapsLock & w::Send ^w
  +
CapsLock & x::Send ^x
  +
CapsLock & y::Send ^y
  +
CapsLock & z::Send ^z
  +
CapsLock & 0::Send ^0
  +
CapsLock & 1::Send ^1
  +
CapsLock & 2::Send ^2
  +
CapsLock & 3::Send ^3
  +
CapsLock & 4::Send ^4
  +
CapsLock & 5::Send ^5
  +
CapsLock & 6::Send ^6
  +
CapsLock & 7::Send ^7
  +
CapsLock & 8::Send ^8
  +
CapsLock & 9::Send ^9
  +
; how to scape ` and ; ?
  +
; CapsLock & `::Send ^`
  +
; CapsLock & ; ::Send ^;
  +
CapsLock & '::Send ^'
  +
CapsLock & ,::Send ^,
  +
CapsLock & .::Send ^.
  +
CapsLock & /::Send ^/
  +
CapsLock & -::Send ^-
  +
CapsLock & =::Send ^=
  +
CapsLock & [::Send ^[
  +
CapsLock & ]::Send ^]
 
</pre>
 
</pre>
   
  +
The following script follows the same idea as the above while helping its shortcomings, and is more concise. It works by checking the last pressed key when the Caps Lock key is released. Hope it helps :)
Swap CapsLock and LeftCtrl:
 
 
<pre>
 
<pre>
  +
; Author: fwompner gmail com
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3A,00,1D,00,1D,00,3A,00,00,00,00,00
 
  +
#InstallKeybdHook
  +
SetCapsLockState, alwaysoff
  +
Capslock::
  +
Send {LControl Down}
  +
KeyWait, CapsLock
  +
Send {LControl Up}
  +
if ( A_PriorKey = "CapsLock" )
  +
{
  +
Send {Esc}
  +
}
  +
return
 
</pre>
 
</pre>
   
  +
Here's an additional solution (which just sends Esc key when Ctrl is pressed alone, but does not modify Caps Lock in any way)
Map LeftAlt as CapsLock, LeftCtrl as LeftAlt, and CapsLock as LeftCtrl (useful for some laptops):
 
 
<pre>
 
<pre>
  +
;Author: Autohotkey forum user RHCP
"Scancode Map"=hex:00,00,00,00,00,00,00,00,04,00,00,00,3A,00,38,00,38,00,1D,00,1D,00,3A,00,00,00,00
 
  +
;http://www.autohotkey.com/board/topic/103174-dual-function-control-key/
  +
$~*Ctrl::
  +
if !state
  +
state := (GetKeyState("Shift", "P") || GetKeyState("Alt", "P") || GetKeyState("LWin", "P") || GetKeyState("RWin", "P"))
  +
return
  +
  +
$~ctrl up::
  +
if instr(A_PriorKey, "control") && !state
  +
send {esc}
  +
state := 0
  +
return
 
</pre>
 
</pre>
   
  +
==HotKeyz==
==Registry scancodes==
 
  +
[http://www.skynergy.com/hotkeyz.html HotKeyz] (freeware) is useful for assigning keyboard shortcuts to run frequently used applications using the <Win> key. It also allows CapsLock to be remapped to any key sequence, or to run an application.
This section is an explanation of the format used for "Scancode Map", from [http://www.microsoft.com/whdc/archive/w2kscan-map.mspx Scan Code Mapper for Windows] and [http://www.computer-engineering.org/ps2keyboard/scancodes1.html Keyboard scan codes].
 
  +
  +
The only disadvantage compared to the registry method is that the status of CapsLock is still set and is checked by applications, so if you press it letting the status LED of the keyboard turn on and then start an application, for example Notepad, you will start typing in uppercase and can't change it anymore. You have then to exit the application, turn the CapsLock status off and re-open the program. This is still easier than rebooting.
   
  +
==Explanations==
More information is at [http://www.usnetizen.com/fix_capslock.php Remap the F12 Key, CapsLock Key or any Key in Windows XP, 2000, Vista and 2003].
 
  +
===Registry scancodes===
  +
The format used for "Scancode Map" in the registry is described below (from [http://www.microsoft.com/whdc/archive/w2kscan-map.mspx Scan Code Mapper for Windows] and [http://www.computer-engineering.org/ps2keyboard/scancodes1.html Keyboard scan codes]). More information is at [http://www.usnetizen.com/fix_capslock.php Remap the F12 Key, CapsLock Key or any Key in Windows XP, 2000, Vista and 2003].
   
 
"Scancode Map" is a registry binary value with the following format (each value is a 32-bit integer in little-endian order):
 
"Scancode Map" is a registry binary value with the following format (each value is a 32-bit integer in little-endian order):
Line 150: Line 269:
 
</pre>
 
</pre>
   
A mapping consists of two 16-bit scancodes. The second scancode (high word) from the keyboard is replaced with the first scancode (low word). Some relevant scancodes:
+
A mapping consists of two 16-bit scancodes. The second scancode (high word) from the keyboard is replaced with the first scancode (low word). Some relevant scancodes are:
 
<pre>
 
<pre>
 
0x01 Esc
 
0x01 Esc
Line 157: Line 276:
 
0x3A CapsLock
 
0x3A CapsLock
 
0x46 ScrollLock
 
0x46 ScrollLock
  +
0x0E Backspace
 
</pre>
 
</pre>
   
For example, the following line:
+
For example, the line
 
<pre>
 
<pre>
 
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00
 
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00
 
</pre>
 
</pre>
   
consists of one mapping (<tt>01,00,3a,00</tt>) which replaces the scancode 0x003a with 0x0001. That means that pressing CapsLock (0x3a) will generate Escape (0x01).
+
consists of one mapping (<code>01,00,3a,00</code>) which replaces the scancode 0x003a with 0x0001. That means that pressing CapsLock (0x3a) will generate Escape (0x01).
  +
  +
==See also==
  +
*[[Talk:Map caps lock to escape in Windows|Discussion page]] for more details and alternatives
  +
*[[VimTip285|Avoid the escape key]] for Vim methods to make pressing Esc easier
  +
  +
==Comments==
  +
What is the "Explanations" section trying to explain? I didn't look in detail (this is a big article) but it doesn't seem to apply directly to the methods listed above. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 19:33, December 15, 2014 (UTC)
  +
:It's explaining what the string of bytes in the registry represent in the "Scancode Map" value in some of the ideas. It applies to the first part of the tip. [[User:JohnBeckett|JohnBeckett]] ([[User talk:JohnBeckett|talk]]) 08:40, December 16, 2014 (UTC)

Revision as of 12:41, 4 January 2015

Tip 75 Printable Monobook Previous Next

created 2001 · complexity basic · author Igor Prischepoff · version 6.0


This tip shows some methods for mapping keys within the operating system to make life easier in Vim. In particular, it can be convenient to use the CapsLock key for Escape so you don't have to reach for the Esc key. It is also useful to use the CapsLock key for Ctrl and press Ctrl-[ instead of Esc.

This tip is for Windows systems (not Windows 9x). For Unix-based systems, see Map caps lock to escape in XWindows.

Some of the procedures here change the way keys behave for all applications. It may be better to use other methods to avoid the escape key in Vim.

Registry

WARNING: Editing your Windows registry may cause unintended side effects that render your system inoperable. Although this tip has worked in the past for some people, there is no guarantee that it will work for you. Use with caution, and at your own risk.

Before trying the following, you should run regedit.exe and inspect the relevant registry keys, and export them for a backup. Make sure you know how to remove the registry changes if they cause trouble.

For the current user (doesn't work in Windows 7 or 8): To map CapsLock to Escape, and ScrollLock to CapsLock, create file (for example) keys.reg containing:

REGEDIT4
[HKEY_CURRENT_USER\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00

Use Explorer to double-click the file to import the value to the registry. To apply the changes, log off and log on. Then, pressing key Esc will generate Escape as normal, pressing CapsLock will also generate Escape, and pressing ScrollLock will generate CapsLock (and you cannot generate ScrollLock).

Alternatively, for the change to apply to all users, run the following reg file and reboot (this key still works in Windows 7 and 8):

REGEDIT4
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,46,00,01,00,3a,00,00,00,00,00

To restore the keyboard, run regedit.exe and delete the "Scancode Map" entry from HKEY_CURRENT_USER (and log off/on), or from HKEY_LOCAL_MACHINE (and reboot), depending on which section of the registry you changed.

Use the Caps Lock key as Ctrl

Particularly while touch typing, it can be useful to remap the keyboard so that pressing CapsLock is the same as pressing Ctrl. This change makes it easy to press Ctrl-[ as an alternative to pressing Esc.

The following applies to the current user (log off and log on to apply). Alternatively, an administrator can apply the change to all users (reboot to apply) by replacing [HKEY_CURRENT_USER\Keyboard Layout] with [HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]

Map CapsLock to LeftCtrl (press CapsLock to generate LeftCtrl):

REGEDIT4
[HKEY_CURRENT_USER\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,1d,00,3a,00,00,00,00,00

An alternative would be to replace the "Scancode Map" line with the following to swap CapsLock and LeftCtrl:

"Scancode Map"=hex:00,00,00,00,00,00,00,00,03,00,00,00,3a,00,1d,00,1d,00,3a,00,00,00,00,00

Utilities

As an alternative to changing the registry, there are various utilities that can remap keys. Many of these can be run by a non-administrator, and can apply mappings only when wanted (for example, when running Vim). They do not require log off/on or reboot to apply changes, unless specified otherwise.

KeyTweak

This tool is a free keyboard remapper for Windows NT/2000/XP/Vista/Win 7. It automates the registry trick above (it makes use of Microsoft's Scancode Map registry key) to remap your keyboard. This app requires you to reboot before changes take effect.

http://webpages.charter.net/krumsick/ (dead link)

http://www.softpedia.com/get/System/OS-Enhancements/KeyTweak.shtml

SharpKeys

This tool is a free keyboard mapper, but requires rebooting the machine for the key mapping to take effect. The interface is fairly simple to use, and appears to work on Windows NT/2003 server/2000/XP/Vista/Win 7. By automating the keyboard mapping, this reduces the risk of the user accidentally changing something in the registry.

http://www.randyrants.com/2008/12/sharpkeys_30.html

AutoHotkey

AutoHotkey is an open source project to automate sending keys and mouse clicks. It can remap keys, and provides a powerful scripting language, and does not require administrator rights. One of the features of this program is that the key mappings are not permanent, but only last as long as the program is running.

To make Capslock work like Escape, add the following to the default script:

Capslock::Esc

The following AutoHotkey script will turn off CapsLock and generate Escape when Esc is pressed:

Esc::
SetCapsLockState, off
Suspend On
Send, {ESC}
Suspend Off
return

The following AutoHotkey script maps CapsLock to Ctrl in PuTTY. In other applications, CapsLock works normally.

classname = ""
keystate = ""

*CapsLock::
 WinGetClass, classname, A
 if (classname = "PuTTY")
   send,{Ctrl down}
 else
   GetKeyState, keystate, CapsLock, T
 if (keystate = "D")
   SetCapsLockState, Off
 else
   SetCapsLockState, On
 return

*CapsLock up::
 WinGetClass, classname, A
 if (classname = "PuTTY")
   send,{Ctrl up}
 return

The following AutoHotkey script maps CapsLock to ESC in Vim. In other applications, CapsLock works normally.

classname = ""
keystate = ""

*Capslock::
  WinGetClass, classname, A
  if (classname = "Vim")
  {
    SetCapsLockState, Off
    Send, {ESC}
  }
  else
  {
    GetKeyState, keystate, CapsLock, T
    if (keystate = "D")
      SetCapsLockState, Off
    else
      SetCapsLockState, On
    return
  }
  return

The following AutoHotKey script switches CapsLock and Escape. When the application is shut down, the keys return to their normal behavior:

CapsOn=false

Capslock::
;suspend to prevent calling esc
Suspend on
Send, {ESC}
Suspend off
return

Esc::
;use global variable to keep track of state
if CapsOn = false
{
 CapsOn = true
 SetCapsLockState, on
}
else
{
 CapsOn = false
 SetCapsLockState, off
}
return

The following AutoHotKey script tries to emulate an idea I read about from Steve Losh where he overloaded his "Caps Lock" key to do the following: when pressing it and another key the "Caps Lock" key is equivalent to a "Control", but when just pressing and releasing the "Caps Lock" quickly it sends an "Escape". I believe that this is the best of the both worlds! In his case, since he uses a Macbook, he was using the software "KeyRemap4Macbook", but the equivalent doesn't exist for Windows (that I know of). However, AutoHotkey can emulate this functionality.

; Sends Esc if capslock is pressed alone
; Sends Ctrl+key if capslock is pressed with another key
; TODO: work with arrow keys and other modifiers (e.g. shift, control)

SetCapsLockState AlwaysOff
CapsLock::Send {esc}
CapsLock & a::Send ^a
CapsLock & b::Send ^b
CapsLock & c::Send ^c
CapsLock & d::Send ^d
CapsLock & e::Send ^e
CapsLock & f::Send ^f
CapsLock & g::Send ^g
CapsLock & h::Send ^h
CapsLock & i::Send ^i
CapsLock & j::Send ^j
CapsLock & k::Send ^k
CapsLock & l::Send ^l
CapsLock & m::Send ^m
CapsLock & n::Send ^n
CapsLock & o::Send ^o
CapsLock & p::Send ^p
CapsLock & q::Send ^q
CapsLock & r::Send ^r
CapsLock & s::Send ^s
CapsLock & t::Send ^t
CapsLock & u::Send ^u
CapsLock & v::Send ^v
CapsLock & w::Send ^w
CapsLock & x::Send ^x
CapsLock & y::Send ^y
CapsLock & z::Send ^z
CapsLock & 0::Send ^0
CapsLock & 1::Send ^1
CapsLock & 2::Send ^2
CapsLock & 3::Send ^3
CapsLock & 4::Send ^4
CapsLock & 5::Send ^5
CapsLock & 6::Send ^6
CapsLock & 7::Send ^7
CapsLock & 8::Send ^8
CapsLock & 9::Send ^9
; how to scape ` and ; ?
; CapsLock & `::Send ^`
; CapsLock & ; ::Send ^;
CapsLock & '::Send ^'
CapsLock & ,::Send ^,
CapsLock & .::Send ^.
CapsLock & /::Send ^/
CapsLock & -::Send ^-
CapsLock & =::Send ^=
CapsLock & [::Send ^[
CapsLock & ]::Send ^]

The following script follows the same idea as the above while helping its shortcomings, and is more concise. It works by checking the last pressed key when the Caps Lock key is released. Hope it helps :)

; Author: fwompner gmail com
#InstallKeybdHook
SetCapsLockState, alwaysoff
Capslock::
Send {LControl Down}
KeyWait, CapsLock
Send {LControl Up}
if ( A_PriorKey = "CapsLock" )
{
	Send {Esc}
}
return

Here's an additional solution (which just sends Esc key when Ctrl is pressed alone, but does not modify Caps Lock in any way)

;Author: Autohotkey forum user RHCP
;http://www.autohotkey.com/board/topic/103174-dual-function-control-key/
$~*Ctrl:: 
if !state 
	state :=  (GetKeyState("Shift", "P") ||  GetKeyState("Alt", "P") || GetKeyState("LWin", "P") || GetKeyState("RWin", "P"))
return 

$~ctrl up::
if instr(A_PriorKey, "control") && !state
	send {esc}
state := 0 
return 

HotKeyz

HotKeyz (freeware) is useful for assigning keyboard shortcuts to run frequently used applications using the <Win> key. It also allows CapsLock to be remapped to any key sequence, or to run an application.

The only disadvantage compared to the registry method is that the status of CapsLock is still set and is checked by applications, so if you press it letting the status LED of the keyboard turn on and then start an application, for example Notepad, you will start typing in uppercase and can't change it anymore. You have then to exit the application, turn the CapsLock status off and re-open the program. This is still easier than rebooting.

Explanations

Registry scancodes

The format used for "Scancode Map" in the registry is described below (from Scan Code Mapper for Windows and Keyboard scan codes). More information is at Remap the F12 Key, CapsLock Key or any Key in Windows XP, 2000, Vista and 2003.

"Scancode Map" is a registry binary value with the following format (each value is a 32-bit integer in little-endian order):

<00000000> <00000000> <NumberFollowingIntegers> <Mapping1> <Mapping2> ... <00000000>

A mapping consists of two 16-bit scancodes. The second scancode (high word) from the keyboard is replaced with the first scancode (low word). Some relevant scancodes are:

0x01 Esc
0x1D L-Ctrl
0x38 L-Alt
0x3A CapsLock
0x46 ScrollLock
0x0E Backspace

For example, the line

"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,01,00,3a,00,00,00,00,00

consists of one mapping (01,00,3a,00) which replaces the scancode 0x003a with 0x0001. That means that pressing CapsLock (0x3a) will generate Escape (0x01).

See also

Comments

What is the "Explanations" section trying to explain? I didn't look in detail (this is a big article) but it doesn't seem to apply directly to the methods listed above. --Fritzophrenic (talk) 19:33, December 15, 2014 (UTC)

It's explaining what the string of bytes in the registry represent in the "Scancode Map" value in some of the ideas. It applies to the first part of the tip. JohnBeckett (talk) 08:40, December 16, 2014 (UTC)