There are many ways a user can input text in your game, for example using a keyboard, an on-screen keyboard or a virtual keyboard such as on mobile devices.
TextInputEvent
The cer::TextInputEvent
is raised when the user has pressed any physical or virtual key that produced a Unicode character.
To capture it, handle the event just like any other:
Text input event example
auto text = String();
auto printText = [&] { logInfo("Text: {}", text); };
while (game.tick()) {
game.handleAllEvents(
[&](TextInputEvent e) {
text += e.text;
printText();
},
[&](KeyPressEvent e) {
if (e.key == Key::Backspace) {
text.removeLast();
printText();
}
},
cerIgnoreEvent,
);
}
On-screen keyboards
To show or hide the system’s on-screen keyboard, use the Window::activateOnScreenKeyboard()
and Window::deactivateOnScreenKeyboard()
methods, respectively.
Typing on an on-screen keyboard will produce TextInputEvent
events.
Example
void MyGame::playerInputRequired() {
game.window().activateOnScreenKeyboard();
}
void MyGame::playerInputFinished() {
game.window().deactivateOnScreenKeyboard();
}
On-screen keyboards are typically a mobile-only feature. If the system doesn’t support on-screen keyboards, calling these functions won’t have any effect.