I’m trying to remove only keyboard events from the event queue.? I
think I want to use SDL_PeepEvents(), but from the docs I can’t really
tell how to use it properly.
?
int SDL_PeepEvents(SDL_Event *events, int numevents, SDL_eventaction
action, Uint32 mask);
?
I’m pretty certain that passing SDL_EVENTMASK(SDL_KeyboardEvent) for
’mask’ will return the keyboard events, but…? Specifically, how is
’numevents’ used.? Do I have to know the number of keyboard events I
want beforehand?
numevents is just the size of the array of SDL_Event structures pointed
to by *events. The simplest usage would be
SDL_Event inEvent;
SDL_PeepEvents(&inEvent, 1, SDL_GETEVENT,
SDL_EVENTMASK(SDL_KeyboardEvent));
//do something with inEvent. call the previous line again to get the
next event.
However - if your rationale for getting only keyboard events is that
you’re only interested in keyboard events, you still can’t afford to
ignore the others. Other kinds of events will build up, and eventually
fill the queue. The queue can only hold so much, and when it’s full all
new events will just get ignored!
For this reason, You would be best served by a loop that handles all
events. You can chose to ignore all but keyboard events, but you must
remove them all. Like:
SDL_Event inEvent;
while(SDL_PollEvent(&inEvent)){
if (inEvent.type == SDL_KEYUP || inEvent.type == SDL_KEYDOWN){
//Nick Campbell's time to shine!
} else {
continue; // ignore
}
}
This loop will run until there are no events left, at which point
SDL_PollEvent will return 0 and the rest of your code will run. Call
this loop again when you’re ready for your next batch of events. This
works well in most circumstances (especially if you only anticipate
keyboard input)
For a better explanation of SDL’s event system and a solution if you
anticipate massive numbers of events (network programming for example),
check out Bob Pendleton’s article at
http://gameprogrammer.com/fastevents/fastevents1.html because bob is in
fact the man. (not the man that keeps us down. just a great
programmer-man). If you decide to use Bob’s FastEvent method, the code
above can be made awesome by replacing ‘SDL_PollEvent’ with
’FE_PollEvent’
Hope that helps
phip (also not the man that keeps you down)On Dec 12, 2003, at 4:41 PM, Nick Campbell wrote: