Having an issue with IsKeyDown()

So for some reason out of nowhere, keyboard input has stopped working. I am checking for keyboard in put using Keyboard.GetState().IsKeyDown() in my Update() method and it isn’t working. IsKeyUp works but not IsKeyDown. It’s really frustrating and stressful as I need to hand this in soon so any help would be super appreciated!

Show some code where you are using is key down

This is top of my Update() method, keyboardState is defined at the top of my class

In fact, no input works, I just tested left button down and it doesn’t work either

Just out of curiosity, just to assuage the possibility that there could be a problem elsewhere in the code, could you place a Debug.WriteLine(“Outside”); outside your input check, and a Debug.Writeline(“Inside”); inside your input check. Run your game in debug mode and let me know if both are showing up in the Output console.

Put a couple breakpoints in your code. See where the code is reachibg

May also help if this is OGL or DX and Desktop or UWP specifically, also what device the issue is occurring on, is it a specialised keyboard on some alienware laptop for example… and are you using some special gaming laptop that may be intercepting your inputs for other features…

hello has anyone found a solution to this?
i have the same problem and i dont know what to do

Make sure your keyboard layout is set to english. This sometimes helps.

it is set to english and it is the default language… but still nothing

If you post code snippet maybe someone can spot a bug.

1 Like

Clone this repo, launch this solution and run one of Samples projects. Try out the buttons there.

here:

KeyboardState kbs = Keyboard.GetState();
public float test;

////
protected override void Update(GameTime gameTime)
    {
        if (kbs.IsKeyDown(Keys.D))
        {
            test++;
        }

        base.Update(gameTime);
    }

and i have a darwstring to show me what is happening

protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.Black);

        _spriteBatch.Begin();

        _spriteBatch.DrawString(font, "t:" + test, new Vector2(110, 100), Color.White);

        _spriteBatch.End();

        base.Draw(gameTime);
    }

and when i press the “D” button nothing happens

ok ill see

KeyboardState is a struct. That means, you cannot instantiate it outside the Update method.

You should use Keyboard.GetState().IsKeyDown(Keys.D) directly.

like this right?

protected override void Update(GameTime gameTime)
    {

        KeyboardState kbs = Keyboard.GetState();

        if (kbs.IsKeyDown(Keys.D))
        {
            test++;
        }

        base.Update(gameTime);
    }

still nothing happens…

Yeah, that’s the code which should work.

And you already tried OpenGL and DirectX?

the monogame project i have is the “Monogame Cross-Platform Desktop Application (OpenGL) (Monogame Team)”

i dont know about directx

There should also be a “MonoGame Windows Desktop Application (Windows DirectX)” Template.

Just want to make sure the Issue is not OpenGL related.

1 Like

OH! yeah! it works now

i guess it was because of the OpenGL
thank you so much <3