SDL_Event(3)
π SDL_Event(3) β the general event structure, bestie π
okay so this one is straight out of the SDL API Reference and honestly? it's the moment. sit down, grab a strawberry π, we're learning about events.
β¨ NAME β¨
SDL_Event β General event structure
π¦ STRUCTURE DEFINITION π¦
here's the whole thing, verbatim, no notes:
typedef union{
Uint8 type;
SDL_ActiveEvent active;
SDL_KeyboardEvent key;
SDL_MouseMotionEvent motion;
SDL_MouseButtonEvent button;
SDL_JoyAxisEvent jaxis;
SDL_JoyBallEvent jball;
SDL_JoyHatEvent jhat;
SDL_JoyButtonEvent jbutton;
SDL_ResizeEvent resize;
SDL_ExposeEvent expose;
SDL_QuitEvent quit;
SDL_UserEvent user;
SDL_SysWMEvent syswm;
} SDL_Event;
π STRUCTURE DATA π
every single member, and what it's giving:
typeβ the type of event πactiveβ activation eventkeyβ keyboard eventmotionβ mouse motion eventbuttonβ mouse button eventjaxisβ joystick axis motion eventjballβ joystick trackball motion eventjhatβ joystick hat motion eventjbuttonβ joystick button eventresizeβ application window resize eventexposeβ application window expose eventquitβ application quit request eventuserβ user defined eventsyswmβ undefined window manager event
πΈ DESCRIPTION πΈ
so the SDL_Event union is the core of all event handling in SDL. no cap. it's probably the most important structure after SDL_Surface, which is saying something. SDL_Event is a union of all the event structures SDL uses, and using it is literally just a matter of knowing which union member goes with which event type. that's it. that's the whole vibe.
here's the matching, bestie β event type on the left, event structure on the right:
SDL_ACTIVEEVENTβSDL_ActiveEventSDL_KEYDOWN/UPβSDL_KeyboardEventSDL_MOUSEMOTIONβSDL_MouseMotionEventSDL_MOUSEBUTTONDOWN/UPβSDL_MouseButtonEventSDL_JOYAXISMOTIONβSDL_JoyAxisEventSDL_JOYBALLMOTIONβSDL_JoyBallEventSDL_JOYHATMOTIONβSDL_JoyHatEventSDL_JOYBUTTONDOWN/UPβSDL_JoyButtonEventSDL_QUITβSDL_QuitEventSDL_SYSWMEVENTβSDL_SysWMEventSDL_VIDEORESIZEβSDL_ResizeEventSDL_VIDEOEXPOSEβSDL_ExposeEventSDL_USEREVENTβSDL_UserEvent
π USE π
the SDL_Event structure has two uses, periodt:
- Reading events on the event queue
- Placing events on the event queue
reading events π«Ά
reading events off the queue is done with either SDL_PollEvent or SDL_PeepEvents. we're gonna use SDL_PollEvent and walk through an example, iykyk.
first off, we create an empty SDL_Event structure.
SDL_Event test_event;
SDL_PollEvent removes the next event from the event queue. if there are no events on the queue it returns 0, otherwise it returns 1. so we use a while loop to process each event in turn.
while(SDL_PollEvent(&test_event)) {
the SDL_PollEvent function takes a pointer to an SDL_Event structure that is to be filled with event information. we know that if SDL_PollEvent removes an event from the queue then the event information will be placed in our test_event structure, but we also know that the type of event will be placed in the type member of test_event. so to handle each event type separately we use a switch statement.
switch(test_event.type) {
we need to know what kind of events we're looking for and the event types of those events. so let's assume we want to detect where the user is moving the mouse pointer within our application. we look through our event types and notice that SDL_MOUSEMOTION is, more than likely, the event we're looking for. a little more research tells us that SDL_MOUSEMOTION events are handled within the SDL_MouseMotionEvent structure, which is the motion member of SDL_Event. we can check for the SDL_MOUSEMOTION event type within our switch statement like so:
case SDL_MOUSEMOTION:
all we need do now is read the information out of the motion member of test_event.
printf("We got a motion event.
");
printf("Current mouse position is: (%d, %d)
", test_event.motion.x, test_event.motion.y);
break;
default:
printf("Unhandled Event!
");
break;
}
}
printf("Event queue empty.
");
placing events π
it is also possible to push events onto the event queue and so use it as a two-way communication path. both SDL_PushEvent and SDL_PeepEvents allow you to place events onto the event queue. this is usually used to place a SDL_USEREVENT on the event queue, however you could use it to post fake input events if you wished. creating your own events is a simple matter of choosing the event type you want, setting the type member and filling the appropriate member structure with information.
SDL_Event user_event;
user_event.type=SDL_USEREVENT;
user_event.user.code=2;
user_event.user.data1=NULL;
user_event.user.data2=NULL;
SDL_PushEvent(&user_event);
π SEE ALSO π
SDL_PollEvent, SDL_PushEvent, SDL_PeepEvents
and that's the whole page, bestie π§ this one came from SDL, dated Tue 11 Sep 2001, 22:59, and it's filed under SDL_Event(3). go forth and poll responsibly πβ¨
π¦ where this page comes from
- debian bookworm: libsdl1.2-dev 1.2.15+dfsg2-8 Β· LGPL-2.1-or-later
- nixos 26.05: SDL_sixel 0-unstable-2016-02-06
- nixpkgs unstable: SDL_sixel 0-unstable-2016-02-06
- ubuntu jammy: libsdl1.2-dev 1.2.15+dfsg2-6 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms