Home arrow Multimedia arrow Page 4 - Game Programming using SDL: Raw Graphics and Event Handling

Handling the Keyboard the SDL Way - Multimedia

If you have found yourself struggling with pixel-level graphics and/or user input for your games, this is the article for you. You will learn how to handle these tasks using SDL. This is the second part of a series on game programming with SDL.

  1. Game Programming using SDL: Raw Graphics and Event Handling
  2. Raw Graphics: Writing Directly onto the Display
  3. Putting the Functions to Use
  4. Handling the Keyboard the SDL Way
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 7
February 05, 2007

print this article



What I've discussed up until now covers only one aspect of providing interactivity. Even now the application doesn't have the ability to handle user gestures provided through different input devices such as a keyboard, joy stick, etc. So now we'll shift our focus to input handling.

Two of the most common input devices are the mouse and the keyboard. SDL has wrappers for each of these. In this section I will discuss keyboard handling. Before entering the world of keyboard events, it is best to understand the most recurring structures in keyboard handling jargon. They are:

  • SDLKey: This is an enumerated type that represents various keys. For example SDLK_a represents a lowercase "a," SDLK_DELETE is for the "delete" key and so on.
  • SDLMod: The SDLKey enumeration represents only keys. To represent key modifiers such as Shift and Ctrl, SDLMod enumeration is provided by SDL. KMOD_CAPS is one of the enumerations that can be used to find out whether the caps key is down or not. Other modifiers also have representations in SDLMod.
  • SDL_keysym: This is a structure that contains the information of a key-press. The members of this structure include scan code in hardware dependent format, the SDLKey value of the pressed key in the sym field, the value of modifier key in the mod field and the Unicode representation of the key in the Unicode field.
  • SDL_KeyboardEvent: From the name itself it is obvious that this structure describes a keyboard event. The first member, type, tells that the event is a key release or a key press event. The second member gives the same info as the first but uses different values. The last member is a structure itself -- the SDL_keysym structure.

Now that the structures have been brought into the picture, the next step is to use these in handling the keyboard events. For this the logic is simple. The SDL_PollEvent is used to read the events. This is placed within the while loop. Then the value of the type member of SDL_Event variable, passed as the parameter to SDL_PollEvent, is checked to find the type of event, and then event processing can be done. In code it looks like this:

SDL_Event event;
/* Poll for events. SDL_PollEvent() returns 0 when  */
/* there are no 
more events on the event queue, our */
/* while loop will exit when 
that occurs. */
while( SDL_PollEvent( &event ) ){
  /* We are only worried about SDL_KEYDOWN and SDL_KEYUP events */
switch( event.type ){
printf( "Key press detectedn" );

printf( "Key release detectedn" );


If this is used this in the program developed in the last article, the exit condition of the program can be controlled. The new version would exit only at a key press.

void display_bmp(char *file_name)
SDL_Surface *image;

/* Load the BMP file into a surface */
image = SDL_LoadBMP(file_name);
if (image == NULL) {
fprintf(stderr, "Couldn't load %s: %sn", file_name, SDL_GetError());

* Palletized screen modes will have a default palette (a
  * standard
8*8*4 colour cube), but if the image is
  * palletized as well we can 
use that palette for a nicer
  * colour matching
if (image->format->palette && screen->format->palette) {
SDL_SetColors(screen, image->format->palette->colors,

  /* Blit onto the screen surface */
if(SDL_BlitSurface(image, NULL, screen, NULL) < 0) {
fprintf(stderr, "BlitSurface error: %sn", SDL_GetError());
SDL_UpdateRect(screen, 0, 0, image->w, image->h);

    /* Free the allocated BMP surface */

  int main(int argc,char* argv[])
/*variable to hold the file name of the image to be loaded
*In real world error handling code would precede this
char* filename="Tux.bmp";

    /*The following code does the initialization for Audio and Video*/
int i_error=SDL_Init(SDL_INIT_VIDEO);

    /*If initialization is unsuccessful, then quit */


* Initialize the display in a 640x480 8-bit palettized mode,
* requesting a software surface

    screen = SDL_SetVideoMode(640, 480, 8, SDL_SWSURFACE);
if ( screen == NULL )
fprintf(stderr, "Couldn't set 640x480x8 video mode: %sn",

    /*Handle the keyboards events here. Catch the SDL_Quit event to exit*/
done = 0;
while (!done)
SDL_Event event;

      /* Check for events */
while (SDL_PollEvent (&event))
switch (event.type)
case SDL_QUIT:
done = 1;

    /* Now call the function to load the image and copy it to the screen surface*/


If you run the above code the window won't be closed until the close button is pressed. Though this code does nothing much in the area of interactivity, it is a beginning. So as you can see, it is really easy to handle keyboard events using SDL. It totally removes the dependence of developers on the operating system  for event handling. Also, as you can see, working at the raw graphics level is not that difficult.

This brings us to the end of this article on SDL programming. The next part will cover using OpenGL with SDL. Till next time.

>>> More Multimedia Articles          >>> More By A.P.Rajshekhar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Basic Lighting in OpenGL and SDL Game Progra...
- Working with Colors in OpenGL for Game Progr...
- Animation in OpenGL for Game Programming usi...
- Game Programming with SDL: Getting Started w...
- Using OpenGL with SDL for Game Programming
- Learning Sound for Game Programming using SDL
- Game Programming using SDL: Raw Graphics and...
- Game Programming using SDL: Getting Started
- Network Radio With Icecast
- Learning To SMILe

Developer Shed Affiliates


Dev Shed Tutorial Topics: