Chunky by FelipeFS
It is currently Mon Feb 08, 2016 7:54 am

All times are UTC

Post new topic  Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Keyboard input with SDL
PostPosted: Wed Oct 10, 2012 7:01 pm 

Joined: Thu Oct 04, 2012 5:45 pm
Posts: 3
I'm having trouble engineering support for keyboard input in SDL for a 2d game. I tried using an SDL event poll, but I couldn't get it to work in a class system. I have a class, eventClass, that contains the function eventLoop that handles my while loop, and another function, movement, that calls the eventLoop function and returns a bool value to movement. movement then performs actions based on those bool values, but at this point it does not respond to key presses. I don't know what im doing wrong exactly, so if anyone can give me some tips on debugging this specific issue, that would be great. If anyone wants to examine specifics in my code, i can post that as well.

PostPosted: Wed Oct 10, 2012 7:57 pm 
Grand Optimizer

Joined: Sun Oct 16, 2011 3:09 pm
Posts: 367
Location: Here (where else?)
It works for me, but I don't use the poll call (I should change to polling one day) See ... in.cpp#130 for my solution.

The only thing I can imagine is that you are not fast enough in pressing a key (that is, the poll has finished already before you actually press the key). If you repeatedly poll however, it should work.
If the above is not the case, you don't give enough details to understand why it fails (for me, at least).

The simplest possible piece of code that demonstrates the problem would be a great help in that case. Code is so much more precise than words :p
My first guess would be that classes are probably not relevant for the problem. If you use threading however, that might be a cause (but I am not sure, I never use threading).

My project: Messing about in FreeRCT, dev blog, and IRC #freerct at

PostPosted: Mon Dec 10, 2012 9:33 pm 
Bit Baby

Joined: Tue May 29, 2012 12:50 pm
Posts: 8
This is a simple way of handling input events. Are you already doing it this way?

Your main game loop (greatly simplified):

while(runMainGameLoop) {
      while(SDL_PollEvent(&event)) {


In the code above, you just replace "eventHandler" with whatever function you want to pass your events to.
Once you've done this, you just need to respond to events properly in this function. If all you need is a regular keypress, an example such as the following would work :

eventHandler(SDL_Event* event) {
   if(event->type == SDL_KEYDOWN) {
      if(event->key.keysym.sym == SDLK_UP) { //this would be if the UP arrow key is pressed
         //do something

A nicer way of doing this however would be with a switch statement:

eventHandler(SDL_Event* event) {
   switch(event->type) {
      case SDL_KEYDOWN: {
         //handle events for individual keypresses here
      //insert additional cases for other event types below (e.g keyup, mousemotion, etc)

You may already be doing this in which case the last 15 minutes of my life have been wasted XD
But if you're following the procedure above your events should be listened to correctly.

If you don't already do this, it can be wise to write a main event class which has virtual functions (create blank functions functions) in it for all the events you need to respond to. Then if you inherit this class in your other classes you just need to override (redefine) the functions you need from that event class. This will give you a flexible way of handling the same input easily in different classes without having to mess around. You'll just need to create an event handler function in each class that uses it that simply calls the main event handler function from your event handling class and passes the event object in. The switch statement from my above examples would go in the main class and just call a relevant function in the class each time an event is actioned.

Phew.. hope that made sense :)

Let me know if this is any help or if you have anymore questions. If you're still stuck, post your code and I'll have a look.

Display posts from previous:  Sort by  
Post new topic  Reply to topic  [ 3 posts ] 

All times are UTC

Who is online

Users browsing this forum: No registered users and 1 guest

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Limited