GetKeyState

  SHORT GetKeyState(nVirtKey)    
  int nVirtKey; /* virtual code */

The GetKeyState function retrieves the status of the specified virtual key. The status specifies whether the key is up, down, or toggled (on, off-alternating each time the key is pressed).

Parameters

nVirtKey

Specifies a the virtual key. If the desired virtual key is a letter or digit (A through Z, a through z, or 0 through 9), nVirtKey must be set to the ASCII value of that character. For other keys, it must be a virtual-key code.

Return Value

The return value specifies the status of the given virtual key. If the high-order bit is 1, the key is down; otherwise, it is up. If the low-order bit is 1, the key is toggled. A toggle key, such as the CAPSLOCK key, is toggled if it has been pressed an odd number of times since the system was started. The key is untoggled if the low-order bit is 0.

Comments

The key status returned from this function changes as a given thread reads key messages from its message queue. The status does not reflect the interrupt level state associated with the physical hardware. Use GetAsyncKeyState to retrieve that information.

An application calls the GetKeyState function in response to a keyboard-input message. This function retrieves the state of the key when the input message was generated.

To obtain state information for all the virtual keys, use the GetKeyboardState function.

An application 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 without distinguishing between left and right. An application can also use the following virtual-key code constants as values ofr nVirtKey to distinguish between the left and right instances of those keys:

VK_LSHIFT VK_RSHIFT
VK_LCONTROL VK_RCONTROL
VK_LMENU VK_RMENU

These left- and right-distinguishing constants are only available to an application through the GetKeyboardState, SetKeyboardState, GetAsyncKeyState, GetKeyState, and MapVirtualKey functions.

See Also

GetAsyncKeyState, GetKeyboardState, MapVirtualKey, SetKeyboardState