Virtual Key Codes

Although some information in lParam might be useful for processing WM_KEYUP, WM_KEYDOWN, WM_SYSKEYUP, and WM_SYSKEYDOWN messages, the wParam parameter is much more important. This parameter contains the ”virtual key code“ that identifies the key that was pressed or released. The developers of Windows have attempted to

VIRTUAL KEY CODES

Decimal
Hex
WINDOWS.H Identifier
Required

IBM Keyboard

1 01 VK_LBUTTON    
2 02 VK_RBUTTON    
3 03 VK_CANCEL * Ctrl-Break
4 04 VK_MBUTTON    
8 08 VK_BACK * Backspace
9 09 VK_TAB * Tab
12 0C VK_CLEAR   Numeric keypad 5 with Num Lock OFF
13 0D VK_RETURN * Enter
16 10 VK_SHIFT * Shift
17 11 VK_CONTROL * Ctrl
18 12 VK_MENU * Alt
19 13 VK_PAUSE   Pause
20 14 VK_CAPITAL * Caps Lock
27 1B VK_ESCAPE * Esc
32 20 VK_SPACE * Spacebar
33 21 VK_PRIOR * Page Up
34 22 VK_NEXT * Page Down
35 23 VK_END   End
36 24 VK_HOME * Home
37 25 VK_LEFT * Left Arrow
38 26 VK_UP * Up Arrow
39 27 VK_RIGHT * Right Arrow
40 28 VK_DOWN * Down Arrow
41 29 VK_SELECT    
42 2A VK_PRINT    
43 2B VK_EXECUTE    
44 2C VK_SNAPSHOT   Print Screen
45 2D VK_INSERT * Insert
46 2E VK_DELETE * Delete
47 2F VK_HELP    
48–57 30–39   * 0 through 9 on main keyboard
65–90 41–5A   * A through Z
96 60 VK_NUMPAD0   Numeric keypad 0 with Num Lock ON
97 61 VK_NUMPAD1   Numeric keypad 1 with Num Lock ON
98 62 VK_NUMPAD2   Numeric keypad 2 with Num Lock ON
99 63 VK_NUMPAD3   Numeric keypad 3 with Num Lock ON
100 64 VK_NUMPAD4   Numeric keypad 4 with Num Lock ON
101 65 VK_NUMPAD5   Numeric keypad 5 with Num Lock ON
102 66 VK_NUMPAD6   Numeric keypad 6 with Num Lock ON
103 67 VK_NUMPAD7   Numeric keypad 7 with Num Lock ON
104 68 VK_NUMPAD8   Numeric keypad 8 with Num Lock ON
105 69 VK_NUMPAD9   Numeric keypad 9 with Num Lock ON
106 6A VK_MULTIPY   Numeric keypad * (enhanced keyboard)
107 6B VK_ADD   Numeric keypad + (enhanced keyboard)
108 6C VK_SEPARATOR    
109 6D VK_SUBTRACT   Numeric keypad - (enhanced keyboard)
110 6E VK_DECIMAL   Numeric keypad
111 6F VK_DIVIDE   Numeric keypad /(enhanced keyboard)
112 70 VK_F1 * Function key F1
113 71 VK_F2 * Function key F2
114 72 VK_F3 * Function key F3
115 73 VK_F4 * Function key F4
116 74 VK_F5 * Function key F5
117 75 VK_F6 * Function key F6
118 76 VK_F7 * Function key F7
119 77 VK_F8 * Function key F8
120 78 VK_F9 * Function key F9
121 79 VK_F10 * Function key F10
122 7A VK_F11   Function key F11 (enhanced keyboard)
123 7B VK_F12   Function Key F12 (enhanced keyboard)
124 7C VK_F13    
125 7D VK_F14    
126 7E VK_F15    
127 7F VK_F16    
144 90 VK_NUMLOCK   Num Lock

define virtual keys in a device-independent manner. For this reason, some virtual key codes cannot be generated on the IBM PC and strict compatibles but may be found on other manufacturer's keyboards.

The virtual key codes you use most often have names defined in WINDOWS.H. The table above shows these names along with the numeric key codes and the IBM PC key that corresponds to the virtual key. Although all keys cause keystroke messages, the table does not include any symbol keys (such as the key with the / and ? symbols). These keys have virtual key codes of 128 and above, and they are often defined differently for international keyboards. You can determine the values of these virtual key codes using the KEYLOOK program that is shown later in this chapter, but normally you should not process keystroke messages for these keys.

An asterisk (*) in the column labeled ”Required“ indicates that the key is mandatory for any Windows implementation. Windows also requires that a keyboard and keyboard driver allow the Shift, Ctrl, and Shift and Ctrl keys together to be combined with all letter keys, all required cursor keys, and all required function keys. The VK_LBUTTON, VK_MBUTTON, and VK_RBUTTON virtual key codes refer to the left, middle, and right buttons of a mouse. However, you will never receive keystroke messages with wParam set to these values. The mouse generates its own messages.