Why does SDL need a text input API?
When I press a key on my keyboard, my program receives a character event, right?
Well, it's not always that simple. Sometimes it can take multiple key presses to produce a character. Sometimes a single key press can produce multiple characters.
Text input is not as simple as it seems, particularly when you consider International users (and you should). It's not hard to figure out why that is when you look at languages like Chinese, Japanese, and Korean. These languages, collectively referred to as the CJK, have thousands of symbols.
It would not be feasible to have a keyboard with over ten-thousand keys, would it? The solution to this is a software input method.
While this provides a good overview, it may not be accurate for all platforms.
There are multiple input method styles which you can read about here. SDL supports "on-the-spot" mode. This has an important implication: the application is responsible for drawing the composition.
So how does SDL handle text input?
First, an example:
#include <SDL3/SDL.h>
#include <SDL3/SDL_main.h>
extern SDL_Window *InitVideo();
extern void Redraw(SDL_Window *window);
extern char *text;
extern const char *composition;
extern Sint32 cursor;
extern Sint32 selection_len;
int main(int argc, char *argv[])
{
SDL_bool done = SDL_FALSE;
SDL_Window *window = InitVideo();/* ... */
SDL_StartTextInput(window);while (!done) {
SDL_Event event;if (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
/* Quit */
done = SDL_TRUE;break;
case SDL_EVENT_TEXT_INPUT:
/* Add new text onto the end of our text */
strcat(text, event.text.text);break;
case SDL_EVENT_TEXT_EDITING:
/*
Update the composition text.
Update the cursor position.
Update the selection length (if any).
*/
composition = event.edit.text;
cursor = event.edit.start;
selection_len = event.edit.length;break;
}
}
Redraw(window);
}
SDL_Quit();
return 0;
}
One important thing to notice is that the application can enable and disable text input arbitrarily with SDL_StartTextInput() and SDL_StopTextInput(). SDL_SetTextInputRect() controls where the Candidate List will open, if supported.
The application will receive an SDL_TextEditingEvent when a composition is changed (or started). This event contains the composition text as well as the cursor position within the composition. It also contains a length member that determines the length of the text selection, if any.
The application will receive an SDL_TextInputEvent when a composition is committed and also for normal (non-IME) text input. Receiving this event implies that a composition has been committed or a composition never began (direct text input).