[SDL] Confused on how to handle (or why) SDL_StartTextInput() ?

Jiang Jiang gzjjgod at gmail.com
Wed Aug 14 01:14:05 PDT 2013

On Wed, Aug 14, 2013 at 8:18 AM, Sparks <ametalspark-SDL2 at yahoo.com> wrote:
> With SDL 1.2, we did the normal(?) way of SDL_EnableUNICODE(1) and then for each character, we received via keyEvent->keysym.unicode, we would add it to our input buffer to make the unicode string.
> With SDL 2.0, both those have been removed, and it mentions to use the new text input API.
> However, I am not clear the exact reason to use said new text input API, instead of using the old method (which would require a workaround since those are gone).
> Let's say the t key is used for communications.
> When we detect that the t key is pressed, we would then start SDL_StartTextInput(), and then in event loop, we would keep getting SDL_TEXTINPUT events as the user types, but since they could be also hitting other hotkeys (like Function keys or tab or...), wouldn't all this be in the unicode string as well, so we would need a way to filter non ASCII (or non readable) keys from that right?
> Then if they want to edit said chat string, (hit backspace or use arrow keys) then we get SDL_TextEditingEvent events on those, is any of that filtered out as well?

In a typical GUI application, the OS will be responsible for telling
you the candidate text (via SDL_TEXTEDITING), you can choose how (and
where) to show it in your UI.

Let's say with an input method I typed "abc" and got unicode character
"X", the SDL application will first receive three SDL_TEXTEDITING
events with 'a', 'ab' and 'abc', then finally receive SDL_TEXTINPUT
event with unicode character 'X'.

During this text compositing process, user can press any arbitrary
keys such as Function, backspace, both the SDL application and OS
input method will receive it and decide whether to deal with these
keys or not. For instance when user press backspace, most input
methods will delete the last candidate character typed and SDL app
will receive a new SDL_TEXTEDITING event (let's say user typed a, b,
backspace, c, then the application will receive 4 events containing
'a', 'ab', 'a', 'ac' each).

> Then finally, when in SDL_StartTextInput() mode, we wait for them to hit the enter key, and then do SDL_StopTextInput(), which would give us the final unicode string correct?

You can wait until the enter key or other events (ESC or mouse click?)
defined by your application.

> This brings up SDL_SetTextInputRect(), just how does this play into things? It says: "Use this function to set the rectangle used to type Unicode text inputs.", is this supposed to be the buffer limit of the text, so, if we wanted 80 chars max, we would set this to 80? Or is this for something else?

SDL_SetTextInputRect() gives the OS a hint for where to show the
candidate text list, since the OS doesn't know where you want to draw
the text you received via SDL_TEXTEDITING event.

- Jiang

More information about the SDL mailing list