Jump to content
  • Advertisement
Sign in to follow this  
Shuger

reading string from SDL input

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, Is there a quick way to convert SDL input keys to ASCII codes? Writing a code converting each keyboard key into ASCII code seems like long and bad way to do this. Moreover SDL documentation says it shouldn't be done since it can cause problems with compability betweeen keyboards. So what is the "right" way?

Share this post


Link to post
Share on other sites
Advertisement
You can use SDL to do this. First, call "SDL_EnableUNICODE( SDL_ENABLE );". When you no longer care about the ascii values you can disable it again using "SDL_EnableUNICODE( SDL_DISABLE );". Here are the helper functions I use:


// Note: SDL_EnableUNICODE(1); needs to be called before the event is received by SDL
// This returns zero to indicate error/not useful key
// otherwise returns the character that this key represents
char getUnicodeValue( const SDL_KeyboardEvent &key )
{
assert( SDL_EnableUNICODE(SDL_QUERY) == SDL_ENABLE );
// magic numbers courtesy of SDL docs :)
const int INTERNATIONAL_MASK = 0xFF80, UNICODE_MASK = 0x7F;

int uni = key.keysym.unicode;

if( uni == 0 ) // not translatable key (like up or down arrows)
{
// probably not useful as string input
// we could optionally use this to get some value
// for it: SDL_GetKeyName( key );
return 0;
}
else if( ( uni & INTERNATIONAL_MASK ) == 0 )
{
if( SDL_GetModState() & KMOD_SHIFT )
{
return static_cast<char>(toupper(uni & UNICODE_MASK));
}
else
{
return static_cast<char>(uni & UNICODE_MASK);
}
}
else // we have a funky international character. one we can't read :(
{
// we could do nothing, or we can just show some sign of input, like so:
// return '?';
return 0;
}
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!