Hello all, I'm trying to make a simple text box with SDL, I have loosely based it off of The Lazy Foo Tutorial on sting input, but this code doesn't seem to work...

if(event.type==SDL_MOUSEBUTTONDOWN){
             if(isIn(event.button.x,event.button.y)){
                     hasFocus=true;
             } else {
                     hasFocus=false;
             }
     }
     if(event.type==SDL_KEYDOWN){
             if(hasFocus){
                     if(event.key.keysym.sym!=SDLK_BACKSPACE){
                             str+=(char)(event.key.keysym.unicode);
                     } else {
                             if(str.length()!=0){
                                       str.erase(str.length()-1);
                             }
                     }
             }
     }

From a debug the focusing works fine, as is the display, but for some reason the character is not getting appended. What can I do to fix this?

Maybe make sure that conversion is going alright?

Output (char) (event.key.keysym.unicode) or something.

ASCII chars in UTF-8 might be something like, {\x00\x<ASCII-code>}, in that case, you're appending \x00 ;). It's my best guess.

Thanks for the reply, I'll check that out in a little bit, and post results.

From the reference:

"... The unicode field is only used when UNICODE translation is enabled with SDL_EnableUNICODE. ... "

I suppose you did that, but below there is code on how to translate that code back to ASCII.

Buuuuttt as he also notes, if you're not using unicode, don't enable it. Why not grab the .sym instead and convert that? Hope it helps anyway.

Well, now I feel dumb... I forgot the SDL_EnableUNICODE call... thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.