Jump to content
  • Advertisement
Sign in to follow this  
Decrius

[SDL] Proper key input: capitals

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

So, I searched the net and found out I had to enable unicode and use the unicode method to see which character is pressed. However, the unicode member in the struct is empty... So I did it this way: SDL event in the loop:
SDL_Event event;
                while (SDL_PollEvent(&event))
                {
                    switch (event.type)
                    {
                        case SDL_QUIT:
                        {
                            done = true;
                            break;
                        }

                        case SDL_KEYDOWN:
                        {
                            switch (event.key.keysym.sym)
                            {
                                case SDLK_ESCAPE:
                                {
                                    done = true;
                                    break;
                                }
                                case SDLK_RETURN:
                                {
                                    SDLNet_TCP_Send(client, typing.c_str(), typing.length() + 1);
                                    history += "Server: " + typing + "\n";
                                    typing.erase();
                                    lines++;
                                    break;
                                }
                                case SDLK_BACKSPACE:
                                {
                                    if (typing.length() != 0)
                                    {
                                        typing.erase(typing.length() - 1);
                                    }
                                    break;
                                }
                                default:
                                {
                                    if (typing.length() < 1024)
                                    {
                                        input((char) event.key.keysym.sym, &typing);
                                    }
                                    break;
                                }
                            }
                            break;
                        }
                    }
                }
The function to handle the input and put the characters into a C++ string:
void input(char chr, string *typing)
{
    if (chr == ' ')
    {
        *typing += chr;
    }
    else if ((chr >= '0') && (chr <= '9'))
    {
        *typing += chr;
    }
    else if ((chr >= 'a') && (chr <= 'z'))
    {
        if (SDL_GetModState() & KMOD_SHIFT)
        {
            chr -= 32;
        }
        *typing += chr;
    }
}
This sort of works. Whenever shift is pressed it will print the capital for normal alphabet characters, also space and number work. But ofcourse, anything besides that doesn't work. I can ofcourse add all of them including their 'capital' (character when you press shift)...but that takes alot of time, and I think that foreign keyboard will not work. Any ideas on this one? Also: when I press shift, it display '0' (left shift, 1 for right shift). Also if I press other keys like Ctrl it displays a number...probably because I use the default: in the switch and have numbers as printable character...anyway to get around this because nothing seemed to work so far. Thanks in advance, Decrius

Share this post


Link to post
Share on other sites
Advertisement
Here is the function I use to do it for me, pieced together from the SDL documentation. It depends on SDL_EnableUNICODE working though.

char getCharValue( 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 )
{
// thanks SiCrane!
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 '?';
}
}




If you could post a minimal, complete example (hint: don't use SDL_ttf or SDL_net or anythihg, just use SDL_WM_SetCaption() to show the current input string), I'll run it and see what results I get.

[edit: updated code as per SiCrane's help]

[Edited by - rip-off on February 4, 2008 1:43:21 PM]

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Here is the function I use to do it for me, pieced together from the SDL documentation. It depends on SDL_EnableUNICODE working though.

When SDL_EnableUNICODE() is working, the key.keysym.unicode member is already upper cased if the shift key is down. (Unless the caps lock is down, in which case it'll be lower cased.)

Share this post


Link to post
Share on other sites
Ah, thank you guys :).

Unicode appeared to work this time :), probably me doing stupid before :/.

Anyways, rip-off, the code works, except that I had to add a IF statement to make sure it won't add a character to a line when only a shift was pressed. This resulted in that the unicode member was empty (0x00) and ended the string I was appending it to.

if(SDL_GetModState() & KMOD_SHIFT)
{
if ((chr & UNICODE_MASK) != 0x00) // <--
{
*typing += (char) toupper(chr & UNICODE_MASK);
}
}


Thank you very much :), another problem solved xD

Decrius

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
Quote:
Original post by rip-off
Here is the function I use to do it for me, pieced together from the SDL documentation. It depends on SDL_EnableUNICODE working though.

When SDL_EnableUNICODE() is working, the key.keysym.unicode member is already upper cased if the shift key is down. (Unless the caps lock is down, in which case it'll be lower cased.)


I wrote that function when I was first learning SDL, so I'll update it now. Thanks.

@Decrius: use a reference for your string parameter:

void input(const SDL_KeyBoardEvent &key, string &typing)
{
char c = getCharValue(key);
if( c )
{
typing += c;
}
}

// in some function

while(SDL_PollEvent(&event))
{
// other cases here
// ...
default:
{
input(event.key,typing);
}
break;
}

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!