SDL_Event (3) Linux Manual Page
SDL_Event – General event structure
Structure Definition
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
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
expose- Application window expose event
quit- Application quit request event
user- User defined event
syswm- Undefined window manager event
Description
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 typeEvent StructureSDL_ACTIVEEVENTSDL_ActiveEventSDL_KEYDOWN/UPSDL_KeyboardEventSDL_MOUSEMOTIONSDL_MouseMotionEventSDL_MOUSEBUTTONDOWN/UPSDL_MouseButtonEventSDL_JOYAXISMOTIONSDL_JoyAxisEventSDL_JOYBALLMOTIONSDL_JoyBallEventSDL_JOYHATMOTIONSDL_JoyHatEventSDL_JOYBUTTONDOWN/UPSDL_JoyButtonEventSDL_QUITSDL_QuitEventSDL_SYSWMEVENTSDL_SysWMEventSDL_VIDEORESIZESDL_ResizeEventSDL_VIDEOEXPOSESDL_ExposeEventSDL_USEREVENTSDL_UserEvent
Use
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 0 1. We use a while
while(SDL_PollEvent(&test_event)) {
SDL_PollEvent SDL_Event SDL_PollEvent test_event type test_event. So to handle each event type switch
switch(test_event.type) {
type‘s of those events. So lets 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 SDL_MOUSEMOTION SDL_MouseMotionEvent motion SDL_Event. We can check for the SDL_MOUSEMOTION type switch
case SDL_MOUSEMOTION:
motion 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.
");
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
