Delay after Keyboard-Input

Hi guys,

i am learning MonoGame now since 2 weeks. It is a very nice framework and exactly the thing i was searching all the time for game development. Sorry, if the answer to my question is to easy. :grin:

Actually i am developing a Space-Shooter. My problem is, that MonoGame is checking the keyboard input too fast, so it is reading a klick multiple times. At the following i added the code i used to switch into/out FullScreen.

if (Keyboard.GetState().IsKeyDown(Keys.F12))
{
Graphics.ToggleFullScreen();
}

Is there a option to check pressed button in aquivalent to https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.control.keypress?view=netframework-4.7.2?
Or should i add a timer to enable a delay.

Thank you very much!

Welcome; I’m glad you’re enjoying MonoGame! MonoGame’s KeyboardState provides the capability to read whether a key is down or not. If you need to check if a key was just pressed, so that the user needs to release it then press it again for something to happen, you’ll need to track the previous state of the keyboard and update it each frame.

Here’s an example of how to do this. Once you’ve got the previous state of the keyboard, you will have to check if the key in that state is up and the key in the current state is down. This means the user just pressed that key on this frame. I hope that helps!

2 Likes

You need to create two KeyboardState:

KeyboardState ks1, ks2;

And theen inside the Update method:

        ks1 = Keyboard.GetState();

        if (ks1.IsKeyDown(Keys.Enter) && ks2.IsKeyUp(Keys.Enter))
        {
            //something
        }

        ks2 = ks1;
2 Likes

Sorry for my late answer. Thank you very much for your help. I fixed my “problem” with the code from pixie91, but this is going the way, as you recommended.

Sorry for my late answer. Thank you very much for your help. I fixed my “problem” with the your code, Very nice.