SDL_Event
NAME STRUCTURE DEFINITION STRUCTURE DATA DESCRIPTION USE SEE ALSO
SDL_Event- General event structure
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_!QuitEvent quit; SDL_!UserEvent user; SDL_SywWMEvent syswm; } SDL_Event;
type The type of event
active Activation event
key Keyboard event
motion Mouse motion event
button Mouse button event
jaxis Joystick axis motion event
jball Joystick trackball motion event
jhat Joystick hat motion event
jbutton Joystick button event
resize Application window resize event
quit Application quit request event
user User defined event
syswm Undefined window manager event
The SDL_Event union is the core to all event handling is SDL, its probably the most important structure after SDL_Surface. SDL_Event is a union of all event structures used in SDL, using it is a simple matter of knowing which union member relates to which event type.
Event type Event Structure
SDL_ACTIVEEVENT
SDL_!ActiveEvent
SDL_KEYDOWN/UP
SDL_!KeyboardEvent
SDL_MOUSEMOTION
SDL_!MouseMotionEvent
SDL_MOUSEBUTTONDOWN/UP
SDL_!MouseButtonEvent
SDL_JOYAXISMOTION
SDL_!JoyAxisEvent
SDL_JOYBALLMOTION
SDL_!JoyBallEvent
SDL_JOYHATMOTION
SDL_!JoyHatEvent
SDL_JOYBUTTONDOWN/UP
SDL_!JoyButtonEvent
SDL_QUIT SDL_!QuitEvent
SDL_SYSWMEVENT
SDL_SysWMEvent
SDL_VIDEORESIZE
SDL_!ResizeEvent
SDL_USEREVENT
SDL_!UserEvent
The SDL_Event structure has two uses
Reading events on the event queue
Placing events on the event queue
Reading events from the event queue is done with either SDL_!PollEvent or SDL_!PeepEvents. We'll use SDL_!PollEvent and step through an example.
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. We use a while loop to process each event in turn.
while(SDL_!PollEvent( The SDL_!PollEvent function take 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 seperately we use a switch statement.
switch(test_event.type) {
All we need do now is read the information out of the motion member of test_event.
printf(
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(
SDL_!PollEvent, SDL_!PushEvent, SDL_!PeepEvents
One page links to SDL_Event(3):