- Community Pick
- Experts Exchange Approved
Background
I'm having to retrain myself on a new keyboard. No big deal, right? In my case, it is. I've been using the same IBM 84-key PC/AT keyboard for over 25 years. Those keyboards were built to last (and last, and last...). Even on secondary computers, like the ones at work, I've always been able to find keyboards that were finger-memory-compatible, such as the NorthGate OmniKey which has the function keys on the left side, where God intended them. In fact, when I heard that NorthGate was discontinuing that line in the mid 90's, I bought five of them. Well those OmniKey keyboards all eventually died; the original IBM keyboard outlasted them all. It had taken more abuse that you can imagine over the years, but it finally had one too many cups of coffee spilled on it. So I had to switch to one of the half-dozen "new" keyboards that were gathering dust in a closet.
In short: For common keystrokes such as Ctrl+C, Ctrl+V, I invariably hit CapsLock and it irritates me beyond belief. Not only does the desired action not happen, but CapsLock turns on and the current selection is replaced by one letter. Arrggggh!
Who among us has not hit that totally useless CapsLock key by accident? In 35 years of keyboarding, I've had a need for the CapsLock key perhaps twice. Among other irritations, it has caused endless tech-support grief relating to user-entry of passwords. That key should have been moved away from the home row decades ago. It's right next to the A key! Sheesh.
I first thought of just popping the key out and sticking a wad of chewing gum in there. But I found a way to remap it -- make it into a clone of the Ctrl key.
Remapping CapsLock to be Ctrl
There is a little-known entry in the system registry that is checked by the lowest-level keyboard driver before the keystrokes get sent to the rest of the system:
HKEY_LOCAL_MACHINE\SYSTEM\
Scancode Map REG_BINARY <binary data, in hex>
The Scancode Map value probably does not exist on your computer. So it needs to be added. The format is very specific. First, I'll show an example, then I'll provide a full description. Here's a .REG file that remaps CapsLock to Ctrl:
- "" title="CapsLockToCtrl.REG"
]
The hex data is in five groups of four bytes:
00,00,00,00 -- header version (always 00000000)
00,00,00,00 -- header flags (always 00000000)
02,00,00,00 -- number of entries (including terminating NULL)
1d,00,3a,00 -- map entry: desired scancode, key to remap
00,00,00,00 -- NULL terminator
The desired scancode is "Left-Ctrl" (scancode 0x1d) and the key to remap is CapsLock (scancode 0x3a).
Just copy that text to a text file named, for instance, CapsLockToCtrl.REG, then double-click that file. Note: The change will not go into effect until you reboot.
To undo the change, just use RegEdit to drill down to that Keyboard Layout key and delete the Scancode Map entry.
Remapping Other Keys
First, I want to point out that most of the time it's unwise to make permanent changes to your keyboard mapping. It's just not necessary because there are lots of programs that will let you set up macros so that a Shift-, Ctrl-, and/or Alt-key combination will make the computer take a desired action. For instance, you can write a short program (one or two lines of script), create a shortcut to it, and edit the properties of that shortcut to assign it a keyboard combination.
This registry remapping technique is awkward, since it requires a reboot, and I only recommend it in unique situations (such as attempting to avoiding insanity or suicide if you hit CapsLock even one more time :-)
That, said...
If you want to map more than one key, then you need to change the third item (number of entries) to a higher value and include more of the map entry values. Some scancodes are two bytes, with the lead-in byte being e0 or e1; this is usually used to distinguish keys that are duplicated; for instance, the R-Alt key is 0e,38. But, as we will see, it also appears for dedicated keyboard buttons.
At the bottom of this article, I've included an image of a standard keyboard, followed by a scancode list. You can use this registry technique to map any keys to any other.
A few candidate useless keys you might consider remapping are:
Caps Lock 3a
Num Lock 45
Scroll Lock 46
R-Alt e0,38
R-Ctrl e0,1d
Num / e0,35
Num * 37
Num 5 4c
If you want to make a key totally dead, set the desired scancode to 00,00.
AppCommand keys: Reprogramming Dedicated Buttons
Your keyboard may have a set of "dedicated" buttons that are designed to control multimedia devices. For instance, mine has a bunch that let me set the volume, skip to the next track, etc. These work regardless of what media player I use.
I also have a set of buttons with labels like "Internet" "E-mail" "Search" and even one oddity named "Calculator." In writing this article, I became curious about these keys. It turns out that there is another section of the registry that defines what these keys do.
HKLM\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ AppKey \ num
...and...
HKCU\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ AppKey \ num
The HKCU version affects the key-handling of only the current user, while the HKLM setting sets a default for all users.
You probably already have a set of these defined. When I looked into that key, I saw:
The key numbers under AppKey are not scancodes, but don't worry, I'll provide a list of valid values.
Each of the AppKey\num entries can have just one item. Its name decides what to do and its value must be set depending on that name. For instance, in the figure above, the item name under AppKey number 18 is ShellExecute and the value is the command to execute.
Possible values for the item name are:
- ShellExecute
The value is any command that you can execute by typing in at a DOS prompt or in the Start / Run input box. You can pass arguments to the program, but be sure to wrap the whole thing in quotes. For instance,
ShellExecute REG_SZ "Notepad.Exe MyToDoList.Txt"
- Association
The value entry is a file extension or MIME type. For instance, the item that's associated with my dedicated "Internet" button is:
Association REG_SZ http
The system will run whatever program is associated with that file type or MIME type.
- RegisteredApp
It's not clear how this is (was?) supposed to work. The example in the docs set the value to "mail" which would supposedly open up the application that is registered to handle mail. However, in Vista and Win7, the value for AppKey 15 is association to "mailto" (a MIME handler). My thinking is that "RegisteredApp" must have been deprecated. Another possibility: The registry key HKLM\Software\Clients seems to provide a means for applications to register themselves as the "default app" for some things. If you want to explore this, that's one direction to go (see this blog for some related information). But since you can use ShellExecute to do most anything you want, it seems unnecessary.
Re-tasking the "Browser Favorites" Dedicated Button
That's one of the dedicated buttons that I never use, so I thought I'd have it do something that might be handy: Show the "3DFlip" that was a big deal in Vista, but since Win7 has the more functional Aero Peek, nobody talks much about anymore.
Anyway, the way to bring up 3DFlip is by executing this run command:
Rundll32 dwmApi #105
To re-task my "Browser Favorites" button, I modified the registry like this:
And now when I press that button, I get something like this:
Here's the .Reg script that sets the dedicated "Favorites" button so that it executes the 3DFlip:
- "" title="FavsTo3dFlip.REG"]
Notice that I reassigned AppKey 6... which would be a mystery (why 6?) except that I found that these AppKey values correspond to WM_APPCOMMAND message parameter values in a Windows programming header file. Here's the entire list:
AppCommand values from Winuser.h
After some experimentation, I found that only some of the AppKey values work. Your keyboard would need to have the dedicated key and/or you would need to be running an application program that knows what to do when it gets a WM_APPCOMMAND message.
On the upside, you can experiment with these settings without needing to reboot. The system appears to checks the registry with each keystroke.
So... That's how to make a dedicated key take a special action. What if your keyboard does not have dedicated keys for remapping? Read on!
Putting It All Together
We now have a method to remap one low-level scancode to another one. Might we not remap one of the useless standard keys to do something that would normally be assigned to a dedicated key? Sure!
For conciseness of the example, let's reassign one of those keys to execute the action of the newly-minted AppKey 6. On my keyboard, the * key on the numeric keypad is virtually untouched, so I replaced its scancode with the one that the system expects to have come from the dedicated "Favorites" key.
- "" title="NumStarToFav.REG"]
That RegEdit script sets the NumPad * key to equate to keyboard scancode 66 E0. Alas, 66 e0 does not equate to anything much, but I found it in yet another Microsoft document (see references) and I've listed those dedicated button values at the end of the Scancodes Table, below.
Note that the above .REG file will reset your CapsLock back to CapsLock, since the Scancode Map defines just the one key. Here's how a .REG file that sets both will look:
- "" title="MapMyKeys.REG"]
The third set of four bytes is set to 03, indicating that two keys will be mapped.
It's a real pain to experiment with reassigning key codes like this since a reboot is required for every change. But digging around beneath the surface can be fun and educational and I found it worthwhile.
Keyboard Layout
(Use a Key Number from the image to find Scancode in the Table Below)
And here's a legend that shows the key name/label and scancodes for the numbers in the image. For fun, I've added a couple of columns showing the Symbolic constants that programs should use when handling these keys.
Keyboard Scancode & Symbolic Constant Table
Summary
In this article we saw how to remap keyboard scancodes at the lowest level: We set the useless CapsLock key so that it would act like the Ctrl key. We also saw how to modify the "dedicated" browser and multimedia buttons that might be on your keyboard. Finally, we put the two together so that we could remap any key to act as one of the dedicated keys.
The information here has been verified to be accurate for Windows 7 and Vista. Microsoft documentation indicates that it is also valid for Windows XP and Windows 2000, but I did not test that. Also, keyboard device drivers may vary in handling some keys, but will usually adhere to the specifications for common dedicated keyboard buttons.
References:
Archive: Scan Code Mapper for Windows
http://www.microsoft.com/w
Keyboard Scan Code Specification
http://download.microsoft.
Enhanced Keyboards and Windows
http://www.microsoft.com/w
=-=-=-=-=-=-=-=-=-=-=-=-=-
If you liked this article and want to see more from this author, please click the Yes button near the:
Was this article helpful?
label that is just below and to the right of this text. Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-
by: younghv on 2009-12-27 at 03:41:52ID: 7389
DanRollins -
My "CapsLock" has now gone 'beyond' and is in a Universe named 00,00.
I have never had a use for it and this will not only prevent me from accidently locking myself out of password protected accounts, it will also eliminate the ease with which I can FLAME someone with all caps.
This was a real service for a lot of reasons.
Big old yes vote.
younghv