Events, keyboard & mouseSDL 3.2

SDL_GetMouseState

Reads where the mouse is (in the focused window) and which buttons are held.

Signature

SDL_MouseButtonFlags SDL_GetMouseState(float *x, float *y);

How it works

The position is in window coordinates, as floats (SDL3 reports fractions on precise devices). The return value is a bit mask: test a button with buttons & SDL_BUTTON_LMASK (left), SDL_BUTTON_RMASK (right) or SDL_BUTTON_MMASK (middle).

If you use SDL_SetRenderLogicalPresentation, convert the position with SDL_RenderCoordinatesFromWindow before comparing it with your game's coordinates.

Parameters

xfloat *

Where to write x, or NULL.

yfloat *

Where to write y (down is positive), or NULL.

Return value

A mask of held buttons.

Example

example.cpp
1float mx, my;
2SDL_MouseButtonFlags buttons = SDL_GetMouseState(&mx, &my);
3if (buttons & SDL_BUTTON_LMASK) paintAt(mx, my);
Official SDL wiki