How to write text with keyboard

Hello,
sorry for my bad anglish …
I’m new user of Monogame and it’s … Fantastic!

I started to develop a little C# game with Monogame and I need some help for writing texte.

I would like enter the name of player. For that, I can use KeyboardState.GetPressedKeys() and add char by char … but it’s very long.

Exists it an other class for that ? Like Console.readline() in C# ?

1 Like

Hey, welcome to the forum :slight_smile:

For text input there is the well-named GameWindow.TextInput event.
You can add delegates to it from your Game1 implementation with code like the following:

// in Game1.Initialize
Window.TextInput += TextInputHandler;


private void TextInputHandler(object sender, TextInputEventArgs args)
{
    var pressedKey = args.Key;
    var character = args.Character;
    // do something with the character (and optionally the key)
    // ...
}
4 Likes

Oh thanks!
It seems easy to use. I will also use it to change the shortcuts of my game!

I using KeyboardState.GetPressedKeys() but my program is like to nuclear factory haha…

1 Like

Hello, Welcome to the forums, don’t worry about your English, you can also optionally write a simple explanation in English and depending on your first language, you can explain it in that too so others who speak the same language can also help and some of us can translate it to try to help too, so, don’t worry about a language barrier, but try to always include English in your help request to ensure more people can help faster.

Happy Coding!

Oh my… I have been working with MonoGame for almost two years and didn’t realize this was there! I cant tell you how much time this saves me… wow. Thank you so much! (a little embarrassed) :smile:

1 Like