Hi everyone, I'm trying to program a game in C++ using SDL and i want to get peoples names. its not working though, and I'm really confused as to why. :?:

i'm using code like the following:
StringInput.h

class StringInput {
private:
std::string str;
SDL_Surface *text;
int x,y,r,g,b;
SDL_Color textColor;
Uint32 uc;
public:
StringInput(int x, int y);
...
handleInput();
...
};

StringInput.cpp

StringInput::StringInput(int x, int y) {
...
SDL_EnableUNICODE(1);
}
...
void StringInput::handleInput() {
...

if(event.key.keysym.sym!=SDLK_BACKSPACE){

str+=(char)(event.key.keysym.unicode);

} else if(str.length()!=0){

str.erase(str.length()-1);

} else { }
...
}
...

At this point I have done a lot of debugging output to a file called stdout.txt

I have figured out that the value event.key.keysym.unicode is 0 all the time when I type something (normally something like jaksl ... )

What could cause this and how to fix it?

This is in a game of mine that I really want to make. I need help badly. :(

Dani AI

Generated

Short troubleshooting summary and practical fixes for the problem where event.key.keysym.unicode always reads zero.

Most common cause: SDL’s Unicode translation isn't enabled (or was enabled too late). In SDL 1.2 the unicode member of SDL_keysym is only filled when Unicode translation is switched on; otherwise it will be zero. Also the Unicode value is only meaningful for key‑press (keydown) events, not key releases. (libsdl.org)

Where to enable and what to check next:

  • Enable SDL’s Unicode translation once, after SDL has been initialized and before you start polling events. If you enable it inside an object that’s constructed after the event loop starts (or after SDL_Init), you can easily miss it. Also confirm your window has keyboard focus and no event filter is swallowing key events. (libsdl.org)

If you are using SDL2 (not SDL 1.2): the old keysym unicode field no longer exists. SDL2 uses a dedicated text-input API — call the text-input start/stop functions and handle SDL_TEXTINPUT / SDL_TEXTEDITING events; the text is delivered as UTF‑8. Example pattern (SDL2):

SDL_StartTextInput();
while (SDL_PollEvent(&e)) {
  if (e.type == SDL_TEXTINPUT) input += e.text.text;   // e.text.text is UTF-8
}
SDL_StopTextInput();

Use SDL_StartTextInput / SDL_TEXTINPUT for reliable international text input on modern SDL. (wiki.libsdl.org)

A few extra tips: in SDL 1.2 the unicode field is a 16‑bit value; ASCII characters (< 0x80) map directly and can be cast safely to char, but extended characters need proper handling. If the problem disappears after moving the “enable Unicode” call, that confirms a timing/initialization issue (as discovered). For step‑by‑step examples and migration notes, Lazy Foo’s text‑input tutorials are a clear, practical reference. (libsdl.org)

Recommended Answers

All 7 Replies

are you sure SDL can handle unicode? It seems C++ doesn't have Unicode support. So try third party libraries!

are you sure SDL can handle unicode? It seems C++ doesn't have Unicode support. So try third party libraries!

I'm using Unicode to test difference between 'a' and 'A'. So yes, it would have unicode. (I'm using GCC for Windows). And what third party libraries could I use?

(are there any other possible solutions?)

I'm using Unicode to test difference between 'a' and 'A'. So yes, it would have unicode. (I'm using GCC for Windows). And what third party libraries could I use?

(are there any other possible solutions?)

Check with POCO Unicode, if you have question ask in

I updated t using the code from Lazy Foo tutorials. In a standalone program it works fine but in this one it doesn't. Any answers?

If you don't mind Try wxWidgets. from version 2.9.x you can use wxBase which have no GUI components Just console. Remember to compile with unicode flag set.

wx is known to work with Unicode "naturally" when compiled with Unicode. also there is .

i failed :)
I put the SDL Enable Unicode call in the object initialization not the program initialization so that's why it didnt work. now it does.
(also changed the typecasted character values to integers, that might've helped.)
stupid me

i failed :)
I put the SDL Enable Unicode call in the object initialization not the program initialization so that's why it didnt work. now it does.
(also changed the typecasted character values to integers, that might've helped.)
stupid me

Glad you resolved it :)

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.