GetKeyState() retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled on or off, alternating each time the key is pressed.
Code:
SHORT GetKeyState(
int nVirtKey
);
The return value specifies the status of the specified virtual key, as follows:
- If the high-order bit = 1, the key is down; otherwise, it is up.
- If the low-order bit = 1, the keyâs toggled. A key, such as âCAP LOCK,â is toggled if itâs turned on. The key is off &untoggled if the low-order bit = 0. A toggle keyâs indicator light (if any) on the keyboard will be on when the key is toggled, and off when the key is untoggled.
The GetKeyState function can be used to check the down state of the following virtual keys:
Code:
VK_CONTROL VK_RCONTROL VK_LCONTROL
VK_MENU VK_RMENU VK_LMENU
VK_SHIFT VK_RSHIFT VK_LSHIFT
The key status returned from this function changes as a specified thread reads key messages from its message queue. The status does not reflect the interrupt-level state associated with the hardware.
Use the GetAsyncKeyState function to retrieve that information.
An app calls GetKeyState in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.
An app. can use the virtual-key code constants VK_SHIFT, VK_CONTROL, and VK_MENU as values for the nVirtKey parameter. This gives the status of the SHIFT, CTRL or ALT keys w/o distinguishing between left &right.
An app can also use the following virtual-key code constants as values for nVirtKey to distinguish between the left & right keys:
Code:
VK_LSHIFT VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU VK_RMENU
These left- & right-distinguishing constants are available to an app only through the GetAsyncKeyState, GetKeyState & MapVirtualKey functions.
The GetKeyboardState function copies the status of the 256 virtual keys to the specified buffer.
Code:
BOOL GetKeyboardState( PBYTE lpKeyState
);
lpKeyState is a pointer to a 256-byte buffer to hold the results.
If the function succeeds, the return value is nonzero.
Code:
Symbolic Hex Dec Keyboard
Constant Val Val Equivalent
VK_SHIFT 0x10 16 SHIFT
VK_LSHIFT 0xA0 160 L. SHIFT
VK_RSHIFT 0xA1 161 R. SHIFT
VK_CONTROL 0x11 17 CTRL
VK_LCONTROL 0xA2 162 L. CTRL
VK_RCONTROL 0xA3 163 R. CTRL
VK_MENU 0x12 18 ALT
VK_LMENU 0xA4 164 L. ALT
VK_RMENU 0xA5 165 R. ALT
VK_PAUSE 0x13 19 PAUSE
VK_ESCAPE 0x1B 27 ESC
VK_RETURN 0x0D 13 ENTER
VK_CAPITAL 0x14 20 CAPS LOCK
VK_NUMLOCK 0x90 144 NUM LOCK
VK_SCROLL 0x91 145 SCROLL LOCK