Windows 7: Remapping a keyboard

Caveat:  This involves editing the registry directly.  If you don’t feel comfortable editing your computer at this low level stop now.  If you want to proceed please make sure that you have a back up of the registry and preferrably a trusted full back up of your computer.

For writing entries on this blog I capture information from various sources in Microsoft OneNote.  I mostly use Linux in the home environment but I haven’t found an equal to OneNote, despite some of its annoyances,  in Linux.  I also have an old PS/2 miniature keyboard with no “Windows” logo key attached to the computer that I use to edit this blog which means that I can’t use the Win-S short cut key provided in OneNote.  The Win-S key does pretty much the same thing as the Snip tool but can paste the captured image straight into the OneNote document being worked on.

Rather than re-train my brain to use the snip tool (which I’d never come across before) I decided to see if it was possible to remap the Win-S key.  It  turns out that re-mapping keyboard scan codes is entirely possible as described in the document contained in the link since Windows Vista onward.

There are apps around that will do the re-mapping for you automatically but why take the easy route when you can take the somewhat harder route of directly editing the registry and learn something in the process?

I chose to remap the Win-S key to the CAPS-LOCK key which under normal circumstances is probably the least used key on anyone’s keyboard. A quick Internet search found another Microsoft document which shows the scan codes required to be re-mapped are:

CAPS = 0x3A

Left Windows Key = 0xE05B

The scan code mappings for Windows are stored in the following Registry key:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout

Note There is also a Keyboard Layouts key (notice the plural form) under the Control key, but that key should not be modified.

Unless you have previously remapped any keys it is likely that the ScanCode registry entry doesn’t exist and needs to be created.

In the Keyboard Layout key, the Scancode Map value must be added. This value is of type REG_BINARY (little Endian format) . Let’s add the necessary registry key as shown below:

From the Start Menu type “regedit” in the search progams and files box and select regedit.exe:

regedit_start

Navigate your way to the key shown in the code box above:

regisrty_editor

Right click on the KeyBoard Layout key and follow the context menu through to the second level menu, select “Binary value” and press enter:

scancode_key

This will enter a zero length string that we will need to complete as shown below:

scancode_key_added

The data format is specified in the following table:

Start offset (in bytes) Size (in bytes) Data
0 4 Header: Version Information
4 4 Header: Flags
8 4 Header: Number of Mappings
12 4 Individual Mapping
Last 4 bytes 4 Null Terminator (0x00000000)

The first and second DWORDS store header information and should be set to all zeroes for the current version of the Scan Code Mapper. The third DWORD entry holds a count of the total number of mappings that follow, including the null terminating mapping. The minimum count would therefore be 1 (no mappings specified). The individual mappings follow the header. Each mapping is one DWORD in length and is divided into two WORD length fields. Each WORD field stores the scan code for a key to be mapped.

In my example of re-mapping the Left-Win-Key to CAPS LOCK would look like this:

Value Interpretation
0x00000000 Header: Version. Set to all zeroes
0x00000000 Header: Flags. Set to all zeroes
0x02000000 Two entries in the map (including null entry)
0x05BE03A00 Left-Win- key –> CAPS LOCK (0xE05B –> 0x3A)
0x00000000 Null terminator

Once the map is stored in the registry, the system must be rebooted for the mappings to take effect. Note that if the mapping of a scan code is necessary on a keypress, the step is performed in user mode just before the scan code is converted to a virtual key. Doing this conversion in user mode can present certain limitations, such as mapping not working correctly when running under Terminal Services.

To edit the key manually right click on the key and select “Modify Binary Data”:

modify_binary_data

This will give you the data entry box as shown:

edit_binary_value

Enter the data directly as shown in the table above or follow the instructions below.

The Registry Key is formatted as shown in the code box below.  This can be copied directly into a text editor and saved as ScanCode.reg

Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Keyboard Layout]
"Scancode Map"=hex:00,00,00,00,00,00,00,00,02,00,00,00,5b,e0,3a,00,00,00,00,00

When finished don’t forget to logout and back in again or restart the computer for the changes to take effect.