GetKeyState
The GetKeyState function returns the state of a Windows virtual key. Use this function to poll the status of keyboard keys or mouse keys.
nState = GetKeyState( Code )
This function returns the state of a Windows virtual key. The returned value is described in the following table. The states Up and Down refer to the state of "normal", non-toggle keys. A toggle key is one that locks down, such as [Caps Lock] or [Num Lock].
Key State Return Values
Key State |
Return Value |
Up |
>= 0 (high order bit = 0) |
Down |
< 0 (high order bit = 1) |
Toggled on |
low order bit = 1 |
Toggled off |
low order bit = 0 |
|
|
The virtual key may be an ASCII key code or a Window virtual key code. An ASCII key code is a number in the range 48 (or 0x30) through 96 (0x5a). For example, to test the letter A, specify Code = 65. These numbers may be found in an ASCII code table. Virtual key codes for non-printable keys like [F1] or [Ctrl] fall outside the range of "normal" characters to use up the number range 0 through 255.
The supported virtual key codes are listed in the table below. The number in the Code column is what you specify as the argument to the GetKeyState function. Consult Windows documentation for further details on virtual keys.
Supported Windows Virtual Keys
Windows Virtual Key |
Code |
Meaning |
VK_LBUTTON |
1 |
Left Mouse Button |
VK_RBUTTON |
2 |
Right Mouse Button |
VK_MBUTTON |
4 |
Middle Mouse Button |
VK_TAB |
9 |
[Tab] key |
VK_RETURN |
13 |
[Enter] (return) key |
VK_SHIFT |
16 |
[Shift] key |
VK_CONTROL |
17 |
[Ctrl] key |
VK_MENU |
18 |
[Alt] (menu) key |
VK_CAPITAL |
20 |
[Caps Lock] toggle key |
VK_ESCAPE |
27 |
[Esc] key |
VK_SPACE |
32 |
[Space] key |
VK_HOME |
36 |
[Home] key |
VK_LEFT |
37 |
[Left] key |
VK_UP |
38 |
[Up] key |
VK_RIGHT |
39 |
[Right] key |
VK_DOWN |
40 |
[Down] key |
VK_INSERT |
45 |
[Insert] toggle key |
VK_DELETE |
46 |
[Delete] key |
VK_HELP |
47 |
[Help] key |
VK_F1 |
112 |
F1 key |
VK_F2 |
113 |
F2 key |
VK_F3 |
114 |
F3 key |
VK_F4 |
115 |
F4 key |
VK_F5 |
116 |
F5 key |
VK_F6 |
117 |
F6 key |
VK_F7 |
118 |
F7 key |
VK_F8 |
119 |
F8 key |
VK_F9 |
120 |
F9 key |
VK_F10 |
121 |
F10 key |
VK_F11 |
122 |
F11 key |
VK_F12 |
123 |
F12 key |
VK_F13 |
124 |
F13 key |
VK_F14 |
125 |
F14 key |
VK_F15 |
126 |
F15 key |
VK_F16 |
127 |
F16 key |
VK_F17 |
128 |
F17 key |
VK_F18 |
129 |
F18 key |
VK_F19 |
130 |
F19 key |
VK_F20 |
131 |
F20 key |
VK_F21 |
132 |
F21 key |
VK_F22 |
133 |
F22 key |
VK_F23 |
134 |
F23 key |
VK_F24 |
135 |
F24 key |
VK_LSHIFT |
160 |
Left [Shift] key |
VK_RSHIFT |
161 |
Right [Shift] key |
VK_LCONTROL |
162 |
Left [Ctrl] key |
VK_RCONTROL |
163 |
Right [Ctrl] key |
VK_LMENU |
164 |
Left [Alt] (menu) key |
VK_RMENU |
165 |
Right [Alt] (menu) key |
|
|
|
The following script fragment returns the key status when the user is holding down the left [Shift] key:
|
-- get the key state |
|
-- result: state = -127 (< 0 so key is pressed) |