# Restoring text input to the default state

**URL:** <https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690>\
**Category:** SDL Development\
**Created:** [February 21, 2023, 4:07pm UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690 "2023-02-21T16:07:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [February 21, 2023, 4:07pm UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690/1 "2023-02-21T16:07:57Z")

</div>

Before calling `SDL_StartTextInput()` SDL2 is in a default state in which characters typed at the keyboard are received in SDL\_TEXTINPUT events but no Input Method Editor (or On Screen Keyboard) is displayed.

Calling `SDL_StartTextInput()` followed by `SDL_StopTextInput()` does not restore that default state, thereafter no SDL\_TEXTINPUT events are received at all. How can the default state be restored without restarting the program?

---

<div class="post-metadata">

**Author:** ![sjr](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sjr/32/5749_2.png) [@sjr](https://discourse.libsdl.org/u/sjr)\
**Post date:** [February 21, 2023, 9:15pm UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690/2 "2023-02-21T21:15:40Z")

</div>

Seems like the bug is that SDL is sending `SDL_TEXTINPUT` events _before_ `SDL_StartTextInput()` has been called. It makes sense that calling `SDL_StopTextInput()` would make it stop sending `SDL_TEXTINPUT` events.

The docs for `SDL_StartTextInput()` make it sound like your app isn’t supposed to get `SDL_TEXTINPUT` events until after you’ve called `SDL_StartTextInput()`:

> This function will start accepting Unicode text input events in the focused  
> SDL window, and start emitting SDL\_TextInputEvent (SDL\_TEXTINPUT) and  
> SDL\_TextEditingEvent (SDL\_TEXTEDITING) events. Please use this function in  
> pair with SDL\_StopTextInput().

edit: the docs for `SDL_StopTextInput()` confirm that no longer receiving `SDL_TEXTINPUT` events after calling it is the intended behavior:

> Stop receiving any text input events.

You can see for yourself in [SDL\_keyboard.h](https://github.com/libsdl-org/SDL/blob/SDL2/include/SDL_keyboard.h)

---

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [February 21, 2023, 10:23pm UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690/3 "2023-02-21T22:23:11Z")

</div>

> [@sjr](#):
>
> Seems like the bug is that SDL is sending `SDL_TEXTINPUT` events _before_ `SDL_StartTextInput()`

It’s far from being a ‘bug’, it’s a key behavior that my app (and I’m sure many others) rely on; I’m certain it’s by design. It’s unfortunate that it doesn’t seem to be documented.

If there isn’t a way to restore that state without restarting the program there should be!

---

<div class="post-metadata">

**Author:** ![sjr](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/sjr/32/5749_2.png) [@sjr](https://discourse.libsdl.org/u/sjr)\
**Post date:** [February 21, 2023, 11:44pm UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690/4 "2023-02-21T23:44:35Z")

</div>

The documented behavior is that you’re supposed to get `SDL_TEXTINPUT` events _after_ you call `SDL_StartTextInput()` and stop getting them after you call `SDL_StopTextInput()`

Why would you call `SDL_StopTextInput()` if you don’t want `SDL_TEXTINPUT` events to stop coming?

> it’s a key behavior that my app (and I’m sure many others) rely on

It’s undocumented behavior, but people using it anyway is probably why it persists.

---

<div class="post-metadata">

**Author:** ![rtrussell](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/rtrussell/32/4768_2.png) [@rtrussell](https://discourse.libsdl.org/u/rtrussell)\
**Post date:** [February 22, 2023, 11:18am UTC](https://discourse.libsdl.org/t/restoring-text-input-to-the-default-state/42690/5 "2023-02-22T11:18:33Z")

</div>

> [@sjr](#):
>
> It’s undocumented behavior, but people using it anyway is probably why it persists.

It’s been documented informally, anyway. For example [here](https://discourse.libsdl.org/t/confused-on-how-to-handle-or-why-sdl-starttextinput/19857) Sam says of `SDL_StartTextInput()` and `SDL_StopTextInput()`: “_If you just want American and Western European language support on desktop systems, you can ignore those functions. They’re for more advanced international input with IMEs and mobile devices._”.

So it’s clear that SDL\_TEXTINPUT events **are** supposed to occur before `SDL_StartTextInput()` is called.
