Search code examples
c#inputxnamonogame

GameWindow in Monogame Cross Platform with OpenTK / Input


I am currently working on a game of mine with Monogame and it is a cross platform project (I would like it to run on Windows, Linux and Mac).I have a problem with the getting the input. I would like to have an event that is reaised when a KEY is pressed and give me the key. And another event when a CHAR is entered. What I am doing atm. I am using the Microsoft.Xna.Framework GameWindow from the main class called engine (The class that comes with monogame.) Text Input:

    public static void Initialize(GameWindow window)
    {
        window.TextInput += CharEnteredEvent;
    }

I Subscribe to the event and when a char is entered this event is raised but when ctrl or other keys are pressed it is not as expected. The GameWindow from Microsoft.Xna.Framework has only this but the GameWindow from OpenTK have more events like TextInput, KeyDown, KeyUp etc. If I use the OpenTK Gamewindow I have everything that I need but Monogame cross platform uses Microsoft.Xna.Framework GameWindow. Any ideas how I can use OpenTK Game window and not the monogame one. BTW the crossplatform projects in monogame come with OpenTK. Thanks in advance. Sorry for the long question.


Solution

  • You should poll the keyboard state using Keyboard.GetState() API. The TextInput event is namely only for text input - it does not provide other keys.

    You aren't supposed to peek into OpenTK when using MonoGame. As a cross-platform framework, MonoGame is meant to abstract such libraries away. For example, the upcoming 3.6 version will use SDL instead of the obsolete OpenTK.