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:
vKey
: A virtual-key code for the key in question. For a list of virtual-key codes, see Virtual-Key Codes.
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.
GetKeyState
Retrieves the status of a specified virtual key.
SHORT GetKeyState(int nVirtKey
);
Parameters:
nVirtKey
: A virtual-key code for the key in question.
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:
- The high-order bit of the return value is set if the key is down.
- The low-order bit of the return value is set if the key is toggled.
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:
lpPoint
: A pointer to aPOINT
structure that receives the screen coordinates of the mouse cursor.
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:
X
: The new x-coordinate of the cursor.Y
: The new y-coordinate of the cursor.
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:
WM_KEYDOWN
,WM_KEYUP
,WM_CHAR
: For keyboard events.WM_LBUTTONDOWN
,WM_LBUTTONUP
,WM_MOUSEMOVE
,WM_RBUTTONDOWN
, etc.: For mouse events.
These messages are processed within the window procedure of a window.
GetAsyncKeyState
) and message-based input handling is crucial for developing robust Windows applications.