1SDL_Event(3) SDL API Reference SDL_Event(3)
2
3
4
6 SDL_Event - General event structure
7
9 typedef union{
10 Uint8 type;
11 SDL_ActiveEvent active;
12 SDL_KeyboardEvent key;
13 SDL_MouseMotionEvent motion;
14 SDL_MouseButtonEvent button;
15 SDL_JoyAxisEvent jaxis;
16 SDL_JoyBallEvent jball;
17 SDL_JoyHatEvent jhat;
18 SDL_JoyButtonEvent jbutton;
19 SDL_ResizeEvent resize;
20 SDL_ExposeEvent expose;
21 SDL_QuitEvent quit;
22 SDL_UserEvent user;
23 SDL_SysWMEvent syswm;
24 } SDL_Event;
25
27 type The type of event
28
29 active Activation event
30
31 key Keyboard event
32
33 motion Mouse motion event
34
35 button Mouse button event
36
37 jaxis Joystick axis motion event
38
39 jball Joystick trackball motion event
40
41 jhat Joystick hat motion event
42
43 jbutton Joystick button event
44
45 resize Application window resize event
46
47 expose Application window expose event
48
49 quit Application quit request event
50
51 user User defined event
52
53 syswm Undefined window manager event
54
56 The SDL_Event union is the core to all event handling is SDL, its prob‐
57 ably the most important structure after SDL_Surface. SDL_Event is a
58 union of all event structures used in SDL, using it is a simple matter
59 of knowing which union member relates to which event type.
60
61 Event type Event Structure
62
63 SDL_ACTIVEEVENT SDL_ActiveEvent
64
65 SDL_KEYDOWN/UP SDL_KeyboardEvent
66
67 SDL_MOUSEMOTION SDL_MouseMotionEvent
68
69 SDL_MOUSEBUTTONDOWN/UP
70 SDL_MouseButtonEvent
71
72 SDL_JOYAXISMOTION SDL_JoyAxisEvent
73
74 SDL_JOYBALLMOTION SDL_JoyBallEvent
75
76 SDL_JOYHATMOTION SDL_JoyHatEvent
77
78 SDL_JOYBUTTONDOWN/UP
79 SDL_JoyButtonEvent
80
81 SDL_QUIT SDL_QuitEvent
82
83 SDL_SYSWMEVENT SDL_SysWMEvent
84
85 SDL_VIDEORESIZE SDL_ResizeEvent
86
87 SDL_VIDEOEXPOSE SDL_ExposeEvent
88
89 SDL_USEREVENT SDL_UserEvent
90
92 The SDL_Event structure has two uses
93
94 · Reading events on the event queue
95
96 · Placing events on the event queue
97
98 Reading events from the event queue is done with either SDL_PollEvent
99 or SDL_PeepEvents. We'll use SDL_PollEvent and step through an example.
100
101 First off, we create an empty SDL_Event structure.
102
103 SDL_Event test_event;
104
105 SDL_PollEvent removes the next event from the event queue, if there
106 are no events on the queue it returns 0 otherwise it returns 1. We use
107 a while loop to process each event in turn.
108
109 while(SDL_PollEvent(&test_event)) {
110
111 The SDL_PollEvent function take a pointer to an SDL_Event structure
112 that is to be filled with event information. We know that if
113 SDL_PollEvent removes an event from the queue then the event informa‐
114 tion will be placed in our test_event structure, but we also know that
115 the type of event will be placed in the type member of test_event. So
116 to handle each event type seperately we use a switch statement.
117
118 switch(test_event.type) {
119
120 We need to know what kind of events we're looking for and the event
121 type's of those events. So lets assume we want to detect where the user
122 is moving the mouse pointer within our application. We look through our
123 event types and notice that SDL_MOUSEMOTION is, more than likely, the
124 event we're looking for. A little more research tells use that
125 SDL_MOUSEMOTION events are handled within the SDL_MouseMotionEvent
126 structure which is the motion member of SDL_Event. We can check for the
127 SDL_MOUSEMOTION event type within our switch statement like so:
128
129 case SDL_MOUSEMOTION:
130
131 All we need do now is read the information out of the motion member of
132 test_event.
133
134 printf("We got a motion event.
135 ");
136 printf("Current mouse position is: (%d, %d)
137 ", test_event.motion.x, test_event.motion.y);
138 break;
139 default:
140 printf("Unhandled Event!
141 ");
142 break;
143 }
144 }
145 printf("Event queue empty.
146 ");
147
148 It is also possible to push events onto the event queue and so use it
149 as a two-way communication path. Both SDL_PushEvent and SDL_PeepEvents
150 allow you to place events onto the event queue. This is usually used to
151 place a SDL_USEREVENT on the event queue, however you could use it to
152 post fake input events if you wished. Creating your own events is a
153 simple matter of choosing the event type you want, setting the type
154 member and filling the appropriate member structure with information.
155
156 SDL_Event user_event;
157
158 user_event.type=SDL_USEREVENT;
159 user_event.user.code=2;
160 user_event.user.data1=NULL;
161 user_event.user.data2=NULL;
162 SDL_PushEvent(&user_event);
163
165 SDL_PollEvent, SDL_PushEvent, SDL_PeepEvents
166
167
168
169SDL Tue 11 Sep 2001, 22:59 SDL_Event(3)