[SDL] What's the best way to implement rebindable keys?

Ryan Hanlon ryanh at uidaho.edu
Mon Sep 9 09:54:01 PDT 2002

I had a similar setup to this one, but I also wanted to make the
keys/buttons as configurable as possible.  So I did something like this:

* Enum a set of function names, in your case, maybe something like this:


* Then create an array like this:

Uint8 KeyMap[SDLK_LAST];  // 350 bytes or so shouldn't be too wasteful.
Uint8 ButtonMap[MAX_BUTTONS];  // MAX_BUTTONS should be set to the number
                           // of buttons on your joystick (optional)

* In your initialization or loading of config settings, you can assign
  certain keys to have certain functions:

KeyMap[SDLK_UP] =     ACTION_UP;
ButtonMap[9] =        ACTION_PAUSE;

* You can also create a 'virtual controller' which is just an array of
  button states:

Uint8 InputDev[ACTION_LAST];	// Number of buttons your game recognizes

* In your game loop when you handle keyboard and joystick events, you
  update your controller state:

if Key down:
  InputDev[KeyMap[<key-code>]] = 1;

if Key up:
  InputDev[KeyMap[<key-code>]] = 0;

if Button down:
  InputDev[ButtonMap[<button>]] = 1;

if Button up:
  InputDev[ButtonMap[<button>]] = 0;

(Axis-checking is the usual code if you're using a joystick)

* When the game needs to know whether the 'up' button or key is pressed,
  you can access InputDev[ACTION_UP], which will be true or false.

All of that stuff could be put in a class and wrapped up more nicely, or
just written better altogether, but it solved a lot of my problems. It
also makes it possible to have several keys perform the same function.
Maybe some players want to select things with 'Enter' and some like to
use the space bar.  Setting both keys to the same ACTION* value will do
that.  It also makes it easier to configure your buttons, since you can
load and set your KeyMap/ButtonMap values from a config file at startup.

Far from the 'best' way, I'm sure, but it does the job.


On Mon, 9 Sep 2002, Tim Allen wrote:

> Hi! I'm writing a little tetris clone to teach myself SDL, and I've run
> across an interesting problem: what's the best way to implement
> rebindable keys?
> You only need eight digital inputs to play Tetris (up, down, left,
> right, spin-left, spin-right, start, and select) which I have in an
> enum. My first attempt at keybinding was to have an array of keysyms
> (int keysyms[8]), but that meant scanning the array every time I got a
> keypress, which meant repeated code in every key-related or
> joystick-related event.
> My next attempt was just for the joystick-button binding - I made an
> array as big as the number of buttons on the first joystick, and put one
> of the Tetris keycodes into the entry for each button that was bound.
> That worked, made a lot of code shorter and easier to read, but there's
> something like 350+ different keysyms that you can get events for, which
> seems a little large just for keybinding.
> Ideally, I'd like to use a hash of the event type and keysym or
> button-number as an index into an array, but that seems a little complex.
> How do you deal with keybinding in your SDL application?
> _______________________________________________
> SDL mailing list
> SDL at libsdl.org
> http://www.libsdl.org/mailman/listinfo/sdl

More information about the SDL mailing list