SDL_MouseButtonEvent Structure
What is the SDL_MouseButtonEvent
structure used for?
The SDL_MouseButtonEvent
structure in SDL is used to handle mouse button events. These events occur when the user presses or releases a mouse button.
The structure provides detailed information about the event, such as which button was pressed, the position of the cursor, and the click state.
Structure Definition
Here is the definition of the SDL_MouseButtonEvent
structure:
typedef struct SDL_MouseButtonEvent {
Uint32 type; // Event type, SDL_MOUSEBUTTONDOWN or SDL_MOUSEBUTTONUP
Uint32 timestamp; // Timestamp of the event
Uint32 windowID; // The window with mouse focus
Uint32 which; // The mouse instance ID
Uint8 button; // The mouse button index
Uint8 state; // SDL_PRESSED or SDL_RELEASED
Uint8 clicks; // 1 for single-click, 2 for double-click
Sint32 x; // X coordinate of the mouse
Sint32 y; // Y coordinate of the mouse
} SDL_MouseButtonEvent;
Key Members
type
: The type of event, eitherSDL_MOUSEBUTTONDOWN
(button pressed) orSDL_MOUSEBUTTONUP
(button released).button
: The mouse button that was pressed or released. Common values includeSDL_BUTTON_LEFT
,SDL_BUTTON_RIGHT
, andSDL_BUTTON_MIDDLE
.state
: Indicates whether the button is pressed (SDL_PRESSED
) or released (SDL_RELEASED
).clicks
: Number of clicks, useful for detecting single or double clicks.x
,y
: The coordinates of the mouse cursor when the event occurred.
Usage Example
Here's an example of handling mouse button events using the SDL_MouseButtonEvent
structure:
#include <SDL.h>
#include <iostream>
void HandleMouseButtonEvent(
const SDL_MouseButtonEvent& buttonEvent) {
if (buttonEvent.state == SDL_PRESSED) {
std::cout << "Mouse button pressed at ("
<< buttonEvent.x << ", "
<< buttonEvent.y << ")\n";
} else if (buttonEvent.state == SDL_RELEASED) {
std::cout << "Mouse button released at ("
<< buttonEvent.x << ", "
<< buttonEvent.y << ")\n";
}
}
int main(int argc, char* argv[]) {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cerr << "SDL_Init Error: "
<< SDL_GetError() << '\n';
return 1;
}
SDL_Window* window = SDL_CreateWindow(
"Mouse Button Event",
100, 100, 640, 480,
SDL_WINDOW_SHOWN
);
if (window == nullptr) {
std::cerr << "SDL_CreateWindow Error: "
<< SDL_GetError() << '\n';
SDL_Quit();
return 1;
}
SDL_Event event;
bool running = true;
while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
} else if (event.type == SDL_MOUSEBUTTONDOWN ||
event.type == SDL_MOUSEBUTTONUP) {
HandleMouseButtonEvent(event.button);
}
}
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Mouse button pressed at (200, 150)
Mouse button released at (200, 150)
Summary
The SDL_MouseButtonEvent
structure is essential for handling mouse button events in SDL. It provides detailed information, including the type of button event, the button involved, the state (pressed or released), the number of clicks, and the cursor's position.
By understanding and utilizing this structure, you can effectively manage mouse interactions in your SDL applications.
Mouse Input Basics
Discover how to process mouse input, including position tracking and button presses