# SDL y Guichan Custom Keys

This topic is 4003 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I am trying to implement letting the player customize the controls. I have a way to store it in an xml file, but I don't know how to actually do it. Here is what I have so far... In the config file its simply a tag that looks like this
<option name="" value="" />

Where name is will be a command(e.g. walk_north). And in the value will be the key (e.g. up_arrow.) Now I don't know the best way to handle this because the user should be able to use any key. The command will get sent to a command handler and it will be handled from there. If the key is not handled by a bind then pass it to Guichan(Most of the gui input will be mouse). One way I just thought of is make the key be the SDL symcode. The only problem I have with this is the user can't customize the controls just by editing the xml file. Should that not be a priority or is using the symcode number a good idea? Any other ideas? I want to avoid huge if else or switch statements. Also, since the symcodes are stored in an enum, how would I write the value to the a string? EDIT: Ok, the reason the SDL symcodes won't work is because i am receiving the input events from Guichan which codes are compleatly different from SDL(even tho thats what its using) I guess I can try the Guichan Codes. But I'm also having a problem because each Config option is stored in a std::list which is in another class. Whats the best way to get the commands stored in the std::list? Loop through them, or is that too much for a time critical moment? [Edited by - Stormtrooper on August 1, 2007 7:06:15 PM]

##### Share on other sites
SDL_GetKeyName(SDLKey) may be of help. I suppose you can use the key names in the XML file, and compare them to an auto-constructed map or so...

std::map<std::string, SDLKey> nkMap;void fillNKMap() {    for (int i = BEGINNING_KEY; i <= ENDING_KEY; ++i)        nkMap[SDL_GetKeyName((SDLKey)i)] = (SDLKey)i;}

(BEGINNING_KEY and ENDING_KEY should be the range of SDLKeys...)

Then you'd do...
std::map<std::string, SDLKey>::iterator i = nkMap.find(keyName);if (i == nkMap.end()) // key name not found    ;else    keyCode = i->second;

Obviously I just wrote this up off the top of my head, but it looks ok.

I'm assuming SDL doesn't have a getKeyCode(name) function, or else I just wrote this all for nothing [grin].

Maybe you can use a compare that ignores capitalization... so that "space" matches "Space", for example.

1. 1
2. 2
3. 3
Rutin
22
4. 4
JoeJ
16
5. 5

• 14
• 29
• 13
• 11
• 11
• ### Forum Statistics

• Total Topics
631774
• Total Posts
3002292
×