When to Use Virtual Keys and Keyboard Scan Codes with AutoHotkey (Beginning Hotkeys Part 14)

Add Missing Keys or Take Advantage of Extra Keyboard Keys with AutoHotkey Scan Codes and Virtual Keys

In preparation for this blog I tested numerous different Hotkey configurations with Virtual Keys and/or keyboard Scan Codes. To say the least, knowing when to use what gets pretty confusing. While many setups might not work, there are multiple other configurations which do the same thing. For that reason, before digging into any details, I’m offering a summary set of thumb rules for figuring out when you might want to use either of these special key codes. (See this previous blog to review Virtual Keys and Scan Codes.)

ComputorEdge AutoHotkey E-Books*         *          *

This beginning Hotkey blog builds upon the discussions in the previous parts. If you find any of the information too confusing, then reviewing earlier blogs may be worthwhile.

New to AutoHotkey? See “Introduction to AutoHotkey: A Review and Guide for Beginners.”

*          *          *

When to Use Virtual Keys or Scan Codes

The following is a set of guidelines for determining when it may be worth your time to include either AutoHotkey Virtual Keys or Scan Codes in your Hotkey setups:

  1. Whenever possible, rather than implementing either Virtual Keys or Scan Codes, use the key characters (e.g. a, b, c or 1, 2, 3) or action names found in the AutoHotkey Key List (e.g. Numpad2, Down, Right). In the vast majority of cases, these AutoHotkey names will do everything you need. Plus, the standard AutoHotkey key names create universal Hotkeys—more likely to work with any Windows computer keyboard.
  2. Use Virtual Keys when there is an action which spans more than one key (e.g. Right and NumpadRight) or there is a missing key action (e.g. RWin, AppsKey, F17).
  3. Use Scan Codes to alter a specific hardware keyboard key. For example, the numeric keypad keys each have the same Scan Code, but different Virtual keys with NumLock either on or off (e.g. Numpad6 and NumpadRight). Using the Scan Code ignores the NumLock position. Use Scan Codes (if available) to alter that extra key on your keyboard. (While Scan Codes may be used to add missing keys, most likely the new Virtual Key will also be required.)

Identifying Virtual Key Codes and Scan Codes through AutoHotkey

While accessing Virtual Keys and Scan Codes with Hotkeys is probably not a beginning level AutoHotkey topic, it’s well worth exploration for those times when the usual approaches to Hotkeys just don’t work. By using these codes it’s possible to create some Hotkeys which otherwise may not be available. But first, we need to know how to find the codes.

You might be able to use the charts referenced in this previous blog, but there are times when you’ll need to uncover the Scan Codes or Virtual Keys for yourself. For that task use the AutoHotkey utility program which you can open with any running AHK file (not the compiled EXE file).

As previous discussed (program image below), open any running .ahk file’s main window with either a double-click on System Tray icon or a right-click on that same icon, then select Open from the context menu. Next, select “Key history and script info” from the View menu at the top of the main AutoHotkey script window.

AHK Open Key History
To view Virtual Key Codes and Scan Codes, 0pen the main window of a running .ahk file (double-click on the System Tray icon or right-click on the same icon and select Open from the menu). Next, select “Key history and script info” from the top View menu. The left-most column displays the Virtual Key Code for any pressed key while the second column displays the Scan Code—both in hexadecimal. The Hotkey key action name used in AutoHotkey scripts appears in the last column and corresponds to the Virtual Key Code. Press F5 to refresh after pressing more keys.

The main program window assists in determining the codes and behavior of various keys. It’s helpful when adding back missing keyboard keys, finding the codes for special function keys, or using that extra key you found on your keyboard.

Adding Back a Missing Key

There are a number of keys which physically only appear on a few keyboards. The AppsKey, which pops up the context menu for active windows, fell out of favor a while back. When the mouse right-click replaced it, the AppsKey disappeared from keyboards. Now, it is rare to find it on any keyboard, although there are times when it would be more convenient than using the right mouse button.

Not many keyboards include a right Windows key (RWin). It seems that a left Windows key (LWin) suffices for most keyboards. However, if you’re left-handed, you may want to bring back the right Windows key for special use, thus saving yourself from the need to hassle with the left Windows key.

Unless you own a massive keyboard, function keys F13 through F24 do not exist on yours. In most cases, you’ll never miss any of these keys, but if you do want one or two of them available, AutoHotkey supports their functionality. You can add any key action to your keyboard generally by assigning the either the Virtual Key or the preferred AutoHotkey key action name found in the key list to the Scan Code for an alternative keyboard key. But first, you need to identify the software Virtual Key or hardware Scan Code.

Suppose you planned to replace the numeric pad 2 key (Numpad2) with the right Windows key (RWin)? You could just use the following:

Numpad2::RWin

This may be all that you need, but it won’t work if the NumLock key is off. Let’s take a look at the codes which appear in the referenced AHK main program window when using the above Hotkey:

VK SC           Key
62 050 h d 1.69 Numpad2 
5C 15C i d 0.00 RWin 
62 050 h u 0.08 Numpad2 
5C 15C i u 0.00 RWin

(VK—Virtual Key, SC—Scan Code, Key—Key Action Name)

When pressing down Numpad2 (Virtual Key 62 and Scan Code 050) AutoHotkey redirects the action to RWin (Virtual Key 5C and Scan Code 15C). For the key to always return RWin regardless or NumLock position use the Scan Code. The AutoHotkey documentation recommends the following format for assigning both the Virtual Key and Scan Code to a new key:

SC050::Send {VK5CSC15C}

This works for all presses of the Numpad2 key with or without NumLock on and tracks the following actions in an open AHK program window:

 VK SC           Key
 62 050 h d 1.24 Numpad2
 5C 15C i d 0.00 RWin
 5C 15C i u 0.00 RWin
 62 050 s u 0.09 Numpad2

After testing various formats for remapping the Numpad2 key to RWin, I determined that the Virtual Key for the right Windows key or the key action name (RWin) is required in the statement. The Scan Code alone for a right Windows key (SC15C) yielded “not found” as the key name. The use of the Virtual Key may be required when adding any missing keyboard key function back with AutoHotkey.

SC050::VK5C

and

SC050::RWin

both work, but not:

SC050::SC15C ;Does not work

It seems that since the key is not physically present on the keyboard, the Scan Code does not automatically map to the key action or Virtual Key. However, (this gets a little complicated) the last Hotkey does give the following results which include the sending of the LControl key:

62 050 h d 0.56 Numpad2
EA 15C i d 0.00 not found
A2 01D i d 0.00 LControl
5C 15C i d 0.00 RWin
A2 01D i u 0.00 LControl
62 050 h u 0.09 Numpad2
EA 15C i u 0.00 not found

Since the Scan Code alone returns the key function as “not found”, the LControl action gets inserted before RWin. While the Hotkey won’t work on its own as RWin, it can be used in combination with a modifier for a new Hotkey:

^RWin::MsgBox, test  ;works with control or other modifier

Go figure. While this is a little enigmatic, it may be of use in other AutoHotkey script situations. You must determine the value of this bit of information (if any) for yourself.

Key Action Name = Virtual Key

Tip: The Virtual Key is usually interchangeable with the AutoHotkey key action name (if one exists). For example, the Virtual Key for the AppsKey is 5D (hex). Using either the AutoHotkey name AppsKey or VK5D should produce the same effect:

Numpad2::VK5D

is the same as:

Numpad2::AppsKey

In both cases, AppsKey is remapped to the Numpad2 key. NumLock must be on for either of these Hotkeys to work. The above technique may be used for adding back the AppsKey or any of the missing function keys (F13 through F24).

However, there are action key names which share the same Virtual Key. For example, both Right and NumpadRight use VK27. This is an important distinction when capturing similar events with different keys.

Virtual Key Vs Scan Code

Whether you use the Virtual Key or Scan Code depends on the situation. To turn Numpad6 key into the AppsKey (regardless) of NumLock position, use the Scan Code for the Numpad6 key:

SC04D::AppsKey

To turn all right arrow cursor keys (Right and NumpadRight) into the AppsKey, use the Virtual Key for Right (VK27):

VK27::Send {AppsKey}

This statement is sensitive to the position of NumLock.

In this case, I was forced to use the Send Command in order to span both keyboard keys. While the numeric right arrow would work with only AppsKey as the action (VK27::AppsKey), the extra right arrow cursor key would not—even though it uses the same Virtual Key. It seems that the Send command will span both keys while a Hotkey remapping will not.

Using An Extra Key on the Keyboard

You may own a keyboard with extra keys. Normally, you’ll be able to identify the codes for that key with the above AutoHotkey program window “Key history” technique. However, sometimes a key is beyond the reach of AutoHotkey.

For example, my laptop and many other keyboards include a Fn (function) key which, when used in combination with other keys, activates additional key actions. When I press the Fn key on my laptop, it doesn’t register in the AutoHotkey program window. However, when used in combination with the top row function keys (F1-F12) it gives a new key with a different Virtual Key and Scan Code. A number of these keys are used for multimedia functions such as Volume_Up, Volume_DownMedia_Play_Pause, Media_Stop, etc.

Yet, some of the special keys, such as screen dimmer (Fn+F2) and screen brighter (Fn+F3), return generic “not found” codes:

VK SC         Key
FF 12B d 8.84 not found  ;Fn+F2
FF 12B u 0.09 not found 
FF 12B d 1.38 not found  ;Fn+F3
FF 12B u 0.09 not found

There is no way to use these “not found” codes directly through AutoHotkey.

AutoHotkey supports other keys showing usable codes, such as Media_Prev (Fn+F9) and Media_Next (Fn+F11):

VK SC         Key
B1 110 d 3.83 Media_Prev  ;Fn+F9
B1 110 u 0.08 Media_Prev 
B0 119 d 3.05 Media_Next  ;Fn+F11
B0 119 u 0.06 Media_Next

By using these codes or the associated action key names, the keys can be reconfigured as Hotkeys. However, you must continue to use the Fn key in combination with the target key to activate that new Hotkey. This technique should work for any unknown key as long as it returns useful codes in the AutoHotkey program window. But, what about unidentified keys?

Take Advantage of Those Extra Unidentified Keys

Many keyboards sport extra keys for special functions. You may want to remap these keys for other uses, but you find the AutoHotkey program window does not return usable codes. No code? Use the utility software which came with the keyboard (or mouse). Whether or not you can reconfigure the keys depends on the manufacturer’s utility software included (or downloaded) with a new keyboard.

As an example of a solution for a similar a problem with a Logitech mouse, see AutoHotkey Tips: Multiple Hotkeys and Using The Extra Mouse Buttons, “Fixing the XButton1 Mouse Problem.” Just like this Logitech mouse, fancy keyboards usually include extra utilities for configuring the keys by adding macro combinations. If so, then your best bet is to use that software in combination with AutoHotkey scripts to access your Hotkeys.

Next time, I plan to offer Hotkeys for precision control of your mouse cursor. Also in the works for a future blog—exploring the confusing issue of using the #InputLevel directive or the SendLevel command to access active Hotkeys with the same or external AutoHotkey scripts.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s