The structure for all events in SDL.
Defined in <SDL3/SDL_events.h>
typedef union SDL_Event
{/**< Event type, shared with all events, Uint32 to cover user events which are not in the SDL_EventType enumeration */
Uint32 type; /**< Common event data */
SDL_CommonEvent common; /**< Display event data */
SDL_DisplayEvent display; /**< Window event data */
SDL_WindowEvent window; /**< Keyboard device change event data */
SDL_KeyboardDeviceEvent kdevice; /**< Keyboard event data */
SDL_KeyboardEvent key; /**< Text editing event data */
SDL_TextEditingEvent edit; /**< Text editing candidates event data */
SDL_TextEditingCandidatesEvent edit_candidates; /**< Text input event data */
SDL_TextInputEvent text; /**< Mouse device change event data */
SDL_MouseDeviceEvent mdevice; /**< Mouse motion event data */
SDL_MouseMotionEvent motion; /**< Mouse button event data */
SDL_MouseButtonEvent button; /**< Mouse wheel event data */
SDL_MouseWheelEvent wheel; /**< Joystick device change event data */
SDL_JoyDeviceEvent jdevice; /**< Joystick axis event data */
SDL_JoyAxisEvent jaxis; /**< Joystick ball event data */
SDL_JoyBallEvent jball; /**< Joystick hat event data */
SDL_JoyHatEvent jhat; /**< Joystick button event data */
SDL_JoyButtonEvent jbutton; /**< Joystick battery event data */
SDL_JoyBatteryEvent jbattery; /**< Gamepad device event data */
SDL_GamepadDeviceEvent gdevice; /**< Gamepad axis event data */
SDL_GamepadAxisEvent gaxis; /**< Gamepad button event data */
SDL_GamepadButtonEvent gbutton; /**< Gamepad touchpad event data */
SDL_GamepadTouchpadEvent gtouchpad; /**< Gamepad sensor event data */
SDL_GamepadSensorEvent gsensor; /**< Audio device event data */
SDL_AudioDeviceEvent adevice; /**< Camera device event data */
SDL_CameraDeviceEvent cdevice; /**< Sensor event data */
SDL_SensorEvent sensor; /**< Quit request event data */
SDL_QuitEvent quit; /**< Custom event data */
SDL_UserEvent user; /**< Touch finger event data */
SDL_TouchFingerEvent tfinger; /**< Pen proximity event data */
SDL_PenProximityEvent pproximity; /**< Pen tip touching event data */
SDL_PenTouchEvent ptouch; /**< Pen motion event data */
SDL_PenMotionEvent pmotion; /**< Pen button event data */
SDL_PenButtonEvent pbutton; /**< Pen axis event data */
SDL_PenAxisEvent paxis; /**< Render event data */
SDL_RenderEvent render; /**< Drag and drop event data */
SDL_DropEvent drop; /**< Clipboard event data */
SDL_ClipboardEvent clipboard;
/* This is necessary for ABI compatibility between Visual C++ and GCC.
Visual C++ will respect the push pack pragma and use 52 bytes (size of
SDL_TextEditingEvent, the largest structure for 32-bit and 64-bit
architectures) for this union, and GCC will use the alignment of the
largest datatype within the union, which is 8 bytes on 64-bit
architectures.
So... we'll add padding to force the size to be the same for both.
On architectures where pointers are 16 bytes, this needs rounding up to
the next multiple of 16, 64, and on architectures where pointers are
even larger the size of SDL_UserEvent will dominate as being 3 pointers.
*/
128];
Uint8 padding[ } SDL_Event;
The SDL_Event structure is the core of all event handling in SDL. SDL_Event is a union of all event structures used in SDL.
This struct is available since SDL 3.1.3.
The SDL_Event structure has two uses:
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(&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. Looking at the table below 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_EVENT_MOUSE_MOTION:
All we need to do now is read the information out of the motion member of test_event.
"We got a motion event.");
SDL_Log("Current mouse position is: (%f, %f)", test_event.motion.x, test_event.motion.y);
SDL_Log(break;
default:
"Unhandled Event!");
SDL_Log(break;
}
}"Event queue empty."); SDL_Log(
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;/* SDL will copy this entire struct! Initialize to keep memory checkers happy. */
SDL_zero(user_event);
user_event.type = SDL_EVENT_USER;2;
user_event.user.code =
user_event.user.data1 = NULL;
user_event.user.data2 = NULL; SDL_PushEvent(&user_event);
Note that SDL_EVENT_USER is just a starting point for event numbers that SDL won't claim for itself, and you should use SDL_RegisterEvents to claim a unique number that other parts of your app won't also claim.
Event Type | Event Structure | SDL_Event Field |
---|---|---|
SDL_EVENT_AUDIO_DEVICE_ADDED, SDL_EVENT_AUDIO_DEVICE_REMOVED, etc | SDL_AudioDeviceEvent | adevice |
SDL_EVENT_CAMERA_DEVICE_ADDED, SDL_EVENT_CAMERA_DEVICE_APPROVED, etc | SDL_CameraDeviceEvent | cdevice |
SDL_EVENT_CLIPBOARD_UPDATE | SDL_ClipboardEvent | clipboard |
SDL_EVENT_DISPLAY_ADDED, SDL_EVENT_DISPLAY_REMOVED, etc | SDL_DisplayEvent | display |
SDL_EVENT_GAMEPAD_ADDED, SDL_EVENT_GAMEPAD_REMOVED, or SDL_EVENT_GAMEPAD_REMAPPED, etc | SDL_GamepadDeviceEvent | gdevice |
SDL_EVENT_GAMEPAD_AXIS_MOTION | SDL_GamepadAxisEvent | gaxis |
SDL_EVENT_GAMEPAD_BUTTON_DOWN, SDL_EVENT_GAMEPAD_BUTTON_UP | SDL_GamepadButtonEvent | gbutton |
SDL_EVENT_GAMEPAD_TOUCHPAD_DOWN, SDL_EVENT_GAMEPAD_TOUCHPAD_MOTION, etc | SDL_GamepadTouchpadEvent | gtouchpad |
SDL_EVENT_GAMEPAD_SENSOR_UPDATE | SDL_GamepadSensorEvent | gsensor |
SDL_EVENT_DROP_BEGIN, SDL_EVENT_DROP_FILE, SDL_EVENT_DROP_TEXT, etc | SDL_DropEvent | drop |
SDL_EVENT_FINGER_MOTION, SDL_EVENT_FINGER_DOWN, SDL_EVENT_FINGER_UP | SDL_TouchFingerEvent | tfinger |
SDL_EVENT_KEYBOARD_ADDED, SDL_EVENT_KEYBOARD_REMOVED | SDL_KeyboardDeviceEvent | kdevice |
SDL_EVENT_KEY_DOWN, SDL_EVENT_KEY_UP | SDL_KeyboardEvent | key |
SDL_EVENT_JOYSTICK_ADDED, SDL_EVENT_JOYSTICK_REMOVED, SDL_EVENT_JOYSTICK_UPDATE_COMPLETE | SDL_JoyDeviceEvent | jdevice |
SDL_EVENT_JOYSTICK_AXIS_MOTION | SDL_JoyAxisEvent | jaxis |
SDL_EVENT_JOYSTICK_BALL_MOTION | SDL_JoyBallEvent | jball |
SDL_EVENT_JOYSTICK_HAT_MOTION | SDL_JoyHatEvent | jhat |
SDL_EVENT_JOYSTICK_BATTERY_UPDATED | SDL_JoyBatteryEvent | jbattery |
SDL_EVENT_JOYSTICK_BUTTON_DOWN, SDL_EVENT_JOYSTICK_BUTTON_UP | SDL_JoyButtonEvent | jbutton |
SDL_EVENT_MOUSE_ADDED, SDL_EVENT_MOUSE_REMOVED | SDL_MouseDeviceEvent | mdevice |
SDL_EVENT_MOUSE_MOTION | SDL_MouseMotionEvent | motion |
SDL_EVENT_MOUSE_BUTTON_DOWN, SDL_EVENT_MOUSE_BUTTON_UP | SDL_MouseButtonEvent | button |
SDL_EVENT_MOUSE_WHEEL | SDL_MouseWheelEvent | wheel |
SDL_EVENT_PEN_PROXIMITY_IN, SDL_EVENT_PEN_PROXIMITY_OUT | SDL_PenProximityEvent | pproximity |
SDL_EVENT_PEN_DOWN, SDL_EVENT_PEN_UP | SDL_PenTouchEvent | ptouch |
SDL_EVENT_PEN_MOTION | SDL_PenMotionEvent | pmotion |
SDL_EVENT_PEN_BUTTON_DOWN, SDL_EVENT_PEN_BUTTON_UP | SDL_PenButtonEvent | pbutton |
SDL_EVENT_PEN_AXIS | SDL_PenAxisEvent | paxis |
SDL_EVENT_QUIT | SDL_QuitEvent | quit |
SDL_EVENT_SENSOR_UPDATE | SDL_SensorEvent | sensor |
SDL_EVENT_TEXT_EDITING | SDL_TextEditingEvent | edit |
SDL_EVENT_TEXT_EDITING_CANDIDATES | SDL_TextEditingCandidatesEvent | edit_candidates |
SDL_EVENT_TEXT_INPUT | SDL_TextInputEvent | text |
SDL_EVENT_USER | SDL_UserEvent | user |
SDL_EVENT_WINDOW_RESIZED, SDL_EVENT_WINDOW_MOVED, etc | SDL_WindowEvent | window |
Other events | SDL_CommonEvent | common |