Input Functions

This section covers Win32 API functions for handling user input, including keyboard, mouse, and other input devices.

Keyboard Input

Functions for retrieving keyboard input and manipulating keyboard state.

GetAsyncKeyState

Determines whether a key is currently being pressed.

SHORT GetAsyncKeyState(
  int vKey
);

Parameters:

Return Value: If the function is successful, the return value is zero if the key is currently lifted, one if the key is currently being pressed, and a value greater than one if the key has been pressed or released since the last keystroke.

Note: This function is intended for use in games and other multimedia applications that need to poll the current state of the keyboard. For applications that need to process keystrokes as they occur, use the keyboard input messages.

GetKeyState

Retrieves the status of a specified virtual key.

SHORT GetKeyState(
  int nVirtKey
);

Parameters:

Return Value: Returns the status of the specified virtual key. The return value is a short integer that specifies the status of the virtual key, given as follows:

Warning: GetKeyState retrieves the status of a key in the context of the currently active window. If you need to know the state of a key regardless of which window is active, use GetAsyncKeyState.

Mouse Input

Functions for retrieving mouse input and cursor position.

GetCursorPos

Retrieves the current position of the mouse cursor, in screen coordinates.

BOOL GetCursorPos(
  LPPOINT lpPoint
);

Parameters:

Return Value: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

SetCursorPos

Moves the cursor to the specified position on the screen.

BOOL SetCursorPos(
  int X,
  int Y
);

Parameters:

Return Value: If the function succeeds, the return value is nonzero. If the function fails, the return value is zero.

Input Event Messages

For more detailed input handling, applications typically rely on message queues and specific input messages such as:

These messages are processed within the window procedure of a window.

Note: Understanding the distinction between polling input functions (like GetAsyncKeyState) and message-based input handling is crucial for developing robust Windows applications.

Related Topics