## Recommended Posts

Hi I have Unit8 *keys = SDL_GetKeyState(NULL); At the moment I can do something like
if(keys[SDLK_UP])
{
//move the player up
}


My problem is I'd like the player to be able to change the keys they use. So for example If I had a text file with UP: SDLK_UP DOWN: SDLK_DOWN LEFT: SDLK_LEFT RIGHT: SDLK_RIGHT The values on the right would be used for the respective actions. If I read SDLK_UP into a char array such as char *key[20]. How cn I compare that with keys which is assigned to GetKeyState?? Any ideas

##### Share on other sites
You can use SDL_GetKeyName() to get the SDL-defied string name of a key...

##### Share on other sites
And then do a string comparison such as

char *keys[20] = SDL_GetKeyName()
char *upKey[20] = "SDLK_UP";

if(strcmp(keys, upKey){
//move player
}

I guess that makes sense.

Cheers

##### Share on other sites
Right. Although, you may want to translate the user specified keys to their SDL values at load time and store the ints instead of the strings. Then you can use integer comparisons instead of string comparisons. Unfortunately, there isn't a function to go from the string to the SDLKey, so you'd have to create some kind of look-up table to do the translations. Do-able, but a bit of work.

##### Share on other sites
Quote:
 Original post by Dave HuntRight. Although, you may want to translate the user specified keys to their SDL values at load time and store the ints instead of the strings. Then you can use integer comparisons instead of string comparisons.

I like this idea - and it is not that hard to do either [wink]. Here is a very quick example of one way to approach this problem -

// Have a map of the keys and translationsstd::map< std::string, int > KeyMap;// Have a function to translatevoid LoadKeyConfig( std::string filename );...// Open the file and read in the string name then int assignedvoid LoadKeyConfig( std::string filename ){	std::ifstream IF;	IF.open( filename.c_str() );	if( !IF.is_open() )	{		abort();	}	int var;	std::string tempstr;	while( IF >> tempstr )	{		IF >> var;		KeyMap[ tempstr ] = var;	}	IF.close();}...        // If we press escape	if( g_keys[ KeyMap["SDLK_ESCAPE"] ] )	{		g_done = 1;	}	// If we press right	if( g_keys[ KeyMap["SDLK_RIGHT"] ] )	{		// Add 5 to the X		X += 5;		// If we have this line, it will only move once per key release		g_keys[ KeyMap["SDLK_RIGHT"] ] = 0;	}...

Now that example is by no means the *best* thing to do, but it is just so you can get an idea of how you can go about this. Here is the sample project that you can run to see it in action. If you open up the Test.txt file, you will see two keys defined. Try changing the escape from 27 to 97 ('a'). You will see when you now hit 'A' it will escape, but when you hit escape it does nothing.

One thing to keep in mind is that you must use 'a'-'z' values for the alpha keys, not 'A'-'Z'. That is just the way SDL works offhand with this process. Take a look at sdl_keysym.h in order to see what the translate values are for each key. Good luck!

- Drew

##### Share on other sites
Well, duh! Of course you could use a map. Anybody knows that. ** scratches his head wondering "why didn't I think of that..." ** ;)

Excellent suggestion, Drew.

##### Share on other sites
The other thing you could do is simply force the user to use the SDL key numbers, then translate that to an enum number in the programm. The user is going to have to look up what you call [insert key] anyway, so might as well make it easy on yourself.

##### Share on other sites
Thanks, guys that's plenty to think about and work with Cheers!

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628300
• Total Posts
2981894

• 9
• 9
• 11
• 10
• 10