Sign in to follow this  

[SDL] Converting keypress data to ASCII...

This topic is 4224 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

I'm using a bitmap font to render text in my application. The coordinates of each letter (in the bitmap) to be displayed are obtained from the ASCII value of that letter. However, the SDL keypress information, which I beleive resides in: event.key.keysym.sym Where 'event' is an arbitrary SDL_Event I defined earlier, isn't in the right format (it's an enum or whatever?). What I want to do is take this information and convert it to its ASCII counterpart. For example: SDLK_a = 97. Currently I've got a massive switch statement that examines every possible key and assigns it to the correct ASCII integer. Surely there is a much more consise and elegant way around this? Cheers

Share this post


Link to post
Share on other sites
I have this handy function:



char getUnicodeValue( const SDL_KeyboardEvent &key )
{
// 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;
}

if( ( uni & INTERNATIONAL_MASK ) == 0 )
{
if( SDL_GetModState() & KMOD_SHIFT )
{
return (char)(toupper(uni & UNICODE_MASK));
}
else
{
return (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 '?';
}
}




All you have to do is use SDL_EnableUNICODE( SDL_ENABLE ); to enable the unicode mapping. when you're done you can optionally use SDL_EnableUNICODE( SDL_DISABLE );

Hope this helps!

Share this post


Link to post
Share on other sites
I know there are the functions ToAscii() and ToAsciiEx() in the Win32 API, which converts a virtual key into its ASCII equivalent. There are also the MapVirtualKey() and MapVirtualKeyEx() methods that can convert virtual keys into scan codes and/or character values.

But... you're using SDL, so not sure if those functions would be of use to you. Maybe you can find an equivalent in the SDL API with knowledge of those functions? Hope that helps somewhat.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
I have this handy function:

*** Source Snippet Removed ***

All you have to do is use SDL_EnableUNICODE( SDL_ENABLE ); to enable the unicode mapping. when you're done you can optionally use SDL_EnableUNICODE( SDL_DISABLE );

Hope this helps!



Thanks to both of you!

The function above accepts a pointer to an SDL_KeyboardEvent, but my compiler doesn't seem to like it: "syntax error before &". I've removed the & for now, and the function seems to return zero at all instances. Perhaps I can clarify with my function:


void Typing(char input[100])
{
static int i = 0;

input[i] = getUnicodeValue(event.key);

input[i + 1] = '\0';
RenderText(input);
SDL_Flip(Screen);
i++;
}


'event.key' is the struct with the keypress information.
input[] is an array of char that stores what gets typed
RenderText(input) takes the input and does wonderful things to it

As you can see, I'm trying to get an ascii value into the input array. It getUnicodeValue is returning 0...

Share this post


Link to post
Share on other sites
Quote:
Original post by Magic Window
Quote:
Original post by rip-off
I have this handy function:

*** Source Snippet Removed ***

All you have to do is use SDL_EnableUNICODE( SDL_ENABLE ); to enable the unicode mapping. when you're done you can optionally use SDL_EnableUNICODE( SDL_DISABLE );

Hope this helps!



Thanks to both of you!

The function above accepts a pointer to an SDL_KeyboardEvent, but my compiler doesn't seem to like it: "syntax error before &". I've removed the & for now, and the function seems to return zero at all instances. Perhaps I can clarify with my function:


void Typing(char input[100])
{
static int i = 0;

input[i] = getUnicodeValue(event.key);

input[i + 1] = '\0';
RenderText(input);
SDL_Flip(Screen);
i++;
}


'event.key' is the struct with the keypress information.
input[] is an array of char that stores what gets typed
RenderText(input) takes the input and does wonderful things to it

As you can see, I'm trying to get an ascii value into the input array. It getUnicodeValue is returning 0...


Sorry, the function above is c++, I will make a c one.

However, as for it always returning 0, have you done this: SDL_EnableUNICODE( SDL_ENABLE ); as per the end of my post? I'm not sure when it has to be enabled, I assume before you call PollEvent at least, if not before the event is received by SDL, in order for the translation to be done.

Have a look here at the docs for SDL_EnableUNICODE, you can see that the return value will indicate wether it has been enabled, allowing you to test whether the function succeeded.

It should work as posted if this function works, except we copy the SDL_Keyboard event, which I don't think is idiomatic in c ( although I may be wrong )

Here is a c version:

char getUnicodeValue( const SDL_KeyboardEvent *key )
{
// 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;
}

if( ( uni & INTERNATIONAL_MASK ) == 0 )
{
if( SDL_GetModState() & KMOD_SHIFT )
{
return (char)(toupper(uni & UNICODE_MASK));
}
else
{
return (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 '?';
}
}




Which you would use like so:


void Typing(char input[100])
{
static int i = 0;

input[i] = getUnicodeValue( &event.key );

input[i + 1] = '\0';

RenderText(input);

SDL_Flip(Screen);

i++;
}



Share this post


Link to post
Share on other sites

This topic is 4224 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this