GetKeyboardState

  BOOL GetKeyboardState(pbKeyState)    
  PBYTE pbKeyState; /* address of array to receive status data */

The GetKeyboardState function copies the status of the 256 virtual-keyboard keys to the specified buffer.

Parameters

pbKeyState

Points to the 256-byte buffer that will receive the status data for each virtual key.

Return Value

The return value is TRUE if the function was successful, or FALSE if an error occurs. Use the GetLastError function to obtain extended error information.

Comments

An application can call this function to retrieve the current status of all the virtual keys on the keyboard. The status changes as a thread removes keyboard messages from its message queue. The status does not change as keyboard messages are posted to the message queue.

When the function returns, each member of the array pointed to by pbKeyState contains status data for a 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.

To obtain status information for an individual key, use the GetKeyState function.

An application can use the virtual key code constants VK_SHIFT, VK_CONTROL and VK_MENU as indices into the buffer pointed to by lpbKeyState. 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 indices 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, GetKeyState, MapVirtualKey, SetKeyboardState