Help with getting user input for controls with SDL

I am trying to get my menu in my game setup to allow the user to set their
keys to what they want. This includes gamepad, mouse, keyboard. I have never
done this before and was wondering how everyone here is doing this?

I am using GuiChan so I have a text box setup to get the user input. So what
I need is SDL to tell me what key, button, ect… was pressed exactly. I am
not sure how to do this with SDL. I was thinking SDL_WaitEvent()? If so, how
do I know what was pressed without setting up a whole lot of cases?

Thanks

I am trying to get my menu in my game setup to allow the user to
set their keys to what they want. This includes gamepad, mouse,
keyboard. I have never done this before and was wondering how
everyone here is doing this?

I am using GuiChan so I have a text box setup to get the user
input. So what I need is SDL to tell me what key, button, ect… was
pressed exactly. I am not sure how to do this with SDL. I was
thinking SDL_WaitEvent()? If so, how do I know what was pressed
without setting up a whole lot of cases?
That sounds like a decent idea to me. The nice thing about SDL is
that it has constant keysyms, which are of their own type. You could
easily find what key was pressed from SDL_WaitEvent(…) by (first
veifying it’s an SDL_KeyEvent, and then) getting the keyEvent.keysym,
setting your own input keysym variable to that keysym, and viola,
you’re ready to rock an roll.

The only difficulty left is SAVING this choice and reloading it when
your app loads. The nearest I can come to this being easy is what
you feared: a long list of cases… Is there an easier way to get a
keysym from a character and vice-versa?

– ScottOn Apr 19, 2007, at 8:39 PM, Mars_999 wrote:

Thanks for the reply. Yeah I would be interested in knowing what everyone
else is doing for this since I can’t seem to find anything on the net about
it, or examples. Basically I want to do what Doom or any FPS game does and
allow the user to click the box and hit a key or mouse button, even the
gamepad and set that control to whatever action they want to. E.g. fire
weapon, run, jump, ect…

Yeah the long list of cases doesn’t sound fun… If everyone else here can
tell me that is what I need to do I guess I will start the finger
pounding!!! :frowning:

Thanks> ----- Original Message -----

From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of Scott Harper
Sent: Thursday, April 19, 2007 10:27 PM
To: A list for developers using the SDL library. (includes SDL-announce)
Subject: Re: [SDL] Help with getting user input for controls with SDL

On Apr 19, 2007, at 8:39 PM, Mars_999 wrote:

I am trying to get my menu in my game setup to allow the user to
set their keys to what they want. This includes gamepad, mouse,
keyboard. I have never done this before and was wondering how
everyone here is doing this?

I am using GuiChan so I have a text box setup to get the user
input. So what I need is SDL to tell me what key, button, ect… was
pressed exactly. I am not sure how to do this with SDL. I was
thinking SDL_WaitEvent()? If so, how do I know what was pressed
without setting up a whole lot of cases?
That sounds like a decent idea to me. The nice thing about SDL is
that it has constant keysyms, which are of their own type. You could
easily find what key was pressed from SDL_WaitEvent(…) by (first
veifying it’s an SDL_KeyEvent, and then) getting the keyEvent.keysym,
setting your own input keysym variable to that keysym, and viola,
you’re ready to rock an roll.

The only difficulty left is SAVING this choice and reloading it when
your app loads. The nearest I can come to this being easy is what
you feared: a long list of cases… Is there an easier way to get a
keysym from a character and vice-versa?

– Scott


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

The only difficulty left is SAVING this choice and reloading it when
your app loads. The nearest I can come to this being easy is what
you feared: a long list of cases… Is there an easier way to get a
keysym from a character and vice-versa?

Is there any reason you can’t just write out the keysym?

[keyboard_settings]
jump=182
fire=188

(so you read your config file in as a text file, convert the values to
ints and store them in an array, and then in your mainloop do something
like…)

typedef enum {
JUMP,
FIRE,
// etc.
total_actions
} Actions;

// This gets filled in with the data from the config file, so
// actions[JUMP] will be the keysym for jumping, etc…
static SDL_keysym actions[total_actions];

//checking for events...
case SDL_KEYDOWN:
    if (event.key.keysym.sym == actions[JUMP])
        make_player_jump();
    else if (event.key.keysym.sym == actions[FIRE])
        make_player_shoot();
    else {
        // not a key player has configured to do something, ignore.
    }
    break;

(or something like that.)

You could go a step further and have an array of structs that contain a
function pointer:

  // however you want to set this up...
  actions[JUMP].sym = 188;
  actions[JUMP].func = make_player_jump;

So your event loop looks like this:

//checking for events...
case SDL_KEYDOWN:
    for (int i = 0; i < total_actions; i++) {
        if actions[i].sym == event.key.keysym.sym)
            actions[i].func();
    }
    break;

Extra credit for restructuring the data to not need a for loop on each
keypress.

(but really, the big block of if-statements are probably just as good.
Even in something with as many buttons as a flight simulator, you aren’t
going to check that many actions.)

–ryan.

good explanation , also an answer to me thnaks

2007/4/21, Ryan C. Gordon :>

The only difficulty left is SAVING this choice and reloading it when
your app loads. The nearest I can come to this being easy is what
you feared: a long list of cases… Is there an easier way to get a
keysym from a character and vice-versa?

Is there any reason you can’t just write out the keysym?

[keyboard_settings]
jump=182
fire=188

(so you read your config file in as a text file, convert the values to
ints and store them in an array, and then in your mainloop do something
like…)

typedef enum {
JUMP,
FIRE,
// etc.
total_actions
} Actions;

// This gets filled in with the data from the config file, so
// actions[JUMP] will be the keysym for jumping, etc…
static SDL_keysym actions[total_actions];

//checking for events…
case SDL_KEYDOWN:
if (event.key.keysym.sym == actions[JUMP])
make_player_jump();
else if (event.key.keysym.sym == actions[FIRE])
make_player_shoot();
else {
// not a key player has configured to do something, ignore.
}
break;

(or something like that.)

You could go a step further and have an array of structs that contain a
function pointer:

 // however you want to set this up...
 actions[JUMP].sym = 188;
 actions[JUMP].func = make_player_jump;

So your event loop looks like this:

//checking for events…
case SDL_KEYDOWN:
for (int i = 0; i < total_actions; i++) {
if actions[i].sym == event.key.keysym.sym)
actions[i].func();
}
break;

Extra credit for restructuring the data to not need a for loop on each
keypress.

(but really, the big block of if-statements are probably just as good.
Even in something with as many buttons as a flight simulator, you aren’t
going to check that many actions.)

–ryan.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


Just say the words , your wish is my command…