When starting in fullscreen mode the keyboard state gets stuck

I experiences this issue with my game. So to diagnose the issue I created a new project.

So a clean new Windows Universal Monogame project (monogame version 3.6.0.1625)

And in the constructor, I changed to look like this.

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            graphics.ToggleFullScreen();

            graphics.ApplyChanges();
            Content.RootDirectory = "Content";
        }

In the update method, I write this.

    protected override void Update(GameTime gameTime)
    {
        // TODO: Add your update logic here
        // TODO: Add your update logic here
        var keyboardState = Keyboard.GetState();
        var mouseState = Mouse.GetState();

        if (mouseState.LeftButton == ButtonState.Pressed)
        {
            System.Diagnostics.Debug.WriteLine("L-Mouse-pressed");
        }
        if (mouseState.LeftButton == ButtonState.Released)
        {
            System.Diagnostics.Debug.WriteLine("L-Mouse-released");
        }

        if (keyboardState.IsKeyDown(Keys.Down))
        {
            System.Diagnostics.Debug.WriteLine("Down-Down");
        }
        if (keyboardState.IsKeyUp(Keys.Down))
        {
            System.Diagnostics.Debug.WriteLine("Down-Up");
        }
        if (keyboardState.IsKeyDown(Keys.F) && _previousKeyboardState.IsKeyUp(Keys.F))
        {
            graphics.ToggleFullScreen();
        }


        _previousKeyboardState = keyboardState;
        base.Update(gameTime);
    }

The following is what I noticed.

When pressing down for like 4 seconds and then release it, it keeps getting the keyboard state with down button still being pressed. Until I interrupt it with another signal.
If I keep the mouse button pressed for longer than 4 seconds this does not happen.

When I start the game in windowed mode or I toggle it to windowed mode from an initial fullscreen and then go to full screen back again, this case of getting stuck in a state that down is pressed after being pressed for longer than 4 seconds does not occur.

I donā€™t think Iā€™m doing anything wrong.

Does anyone else have this problem?
Is it me.
If not is this bug known? Couldnā€™t find anything recent about this on the web.

Isnā€™t it easier with a KeyDown only if you want to go down while it is pressed and stop as soon as it is released?
If you wanna test when it is down and up, you usually use this to limit the movement at sayā€¦ 1 case down

Alkher,

Ik donā€™t think I correctly understand your suggestion.

But the problem is, if you start the program in fullscreen mode. The release of a button is not detected if the key was pressed for longer than 4 seconds. This Does not occur after the screen has been in windowed mode once.

In my sample code you can see that I try to detect when the down key is released

    if (keyboardState.IsKeyUp(Keys.Down))
    {
        System.Diagnostics.Debug.WriteLine("Down-Up");
    }

But in the visual studio debug output It keeps printing ā€œDown-Downā€. Until I pres another button of change the screen to windowed mode. In this case with the letter ā€œFā€.

Iā€™m having the same issue.

I created a new ā€˜MonoGame Windows 10 Universal Projectā€™.

If I start the project in full screen mode, the keyboard input initially works fine, but the key presses become delayed over time. After about a minute the keyboard is unresponsive.
e.g.
Initially, If I hold the Left arrow for a second and then hold Right for a second, the keyboard state will immediately reflect that Iā€™ve released Left and Iā€™m now pressing Right.

After 30 seconds or so, if I hold Left for a second and then hold Right, the keyboard state will continue to show that Iā€™m pressing Left for some time before finally showing that Iā€™m actually pressing Right now. After releasing Right, the keyboard state continues to show that Iā€™m pressing Right for some seconds before reflecting that Iā€™ve actually released it.

After a minute or so, the keyboard state is so delayed itā€™s essentially hung.
No other aspect of the game is affected by the delay, only the keyboard state.

Starting Not in Fullscreen (ie. running windowed) the keyboard state is not affected and everything works as expected.

1 Like

I have been working on a full screen game. Never ran it in windowed mode until reading this thread.

I could never get the keyboard reads to function.

I worked round it by reading async keyboard input which worked but the the reads were not very smooth.

Changing into windowed mode for the game menu then back to full screen for playing solved the problem.

This is definitely a bug. Thanks for the info.

Is there an issue for this on Monogames github? I didnā€™t see one but also havenā€™t gone very far back

Anyone tried with the latest develop build? (3.7.****)

Just tested with 3.7.0.1206-develop and it appears to have been resolved.

Thanks!

2 Likes

Still happening in 3.7.0.1232-develop sadly, tried the version mike mentioned above and itā€™s still happening with that version as well.

Only happens if you do the following:

this.graphics = new GraphicsDeviceManager(this);
this.graphics.ToggleFullScreen();

Youā€™re right, it is still happening for me too, just a lot less severe.
It seems to run fine for a few seconds and then stick for a bit (0.5 seconds?) and then ā€˜catch-upā€™ and run fine for a bit again before once again sticking. It never completely hangs like it does on the release builds.

Although, more worrying with the new develop builds is the random ā€˜hangā€™ on startup :frowning:

Raised an issue on monogameā€™s github and it looks like if you do:

this.graphics = new GraphicsDeviceManager(this)
{
IsFullScreen = true
};

In the Gameā€™s constructor it fixes it, apparently if you do ā€œgraphics.ToggleFullScreen();ā€ in Initialize() it messes things up.

1 Like

I take that back, itā€™s still happening in fullscreen even with the change I listed above :frowning:

This is interesting. Iā€™ve encountered similar keyboard input issues as well. However, gamepad input is also 100% responsive while the keyboard input issues are occurring. Iā€™m not sure that mine are related to the full screen setting, but I will check in a while. Iā€™ve made at least one post here about the issues Iā€™ve encountered, but no one ever responded :frowning:

A little bit of an update on thisā€¦ so I removed the graphics.IsFullScreen = true setting from my Game1 constructor and I saw a differenceā€¦ they keyboard input is now more responsive and it takes longer until it starts becoming unresponsive. It also seems to shift between unresponsiveness and being responsive. Soā€¦ somewhat improved, but still far from ideal. Has anyone else made any progress on this?

This might go all the way down to Windows.UI.Core. I noticed this problem a few months ago in a Win2D project when using CoreWindow functions. Now I just started a MonoGame skeleton project and noticed stuck keys again. A github search shows that MonoGame uses the Windows.UI.Core functions for reading the keyboard.

A possible workaround is to delay full screen until after you call the base class in the first Update():

Set graphics.IsFullScreen = false; in the constructor.
Keep track of the number of update calls or create a bool for this then in Update():.

        base.Update(gameTime);
        if (false == graphics.IsFullScreen && frames == 0) {
                graphics.ToggleFullScreen();
        }
        ++frames;

Why does this work? I donā€™t know.