I’m trying to build a very small example to run input logic but i’m facing a small issue. The code below is runnable with MonoGame v3.7.0.1708.
My problem is that whenever you press a key (here the example is for the D key) i’m displaying a small text on the console.
But you will see if you run the code that the display happens with lag. When i press the “D” key on my keyboard, the message is displayed only ~200ms after the input.
You can for example do a fast press on the keyboard and the message will be displayed after the release of the key.
Does anyone have any idea why it’s happening? Thanks!
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using System;
namespace TestProject
{
public class Game1 : Game
{
GraphicsDeviceManager graphics;
GameInput gameInput;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
gameInput = new GameInput();
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent() {}
protected override void UnloadContent() {}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
base.Update(gameTime);
gameInput.Update();
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
}
}
public class GameInput
{
public bool _dKeyPressed = false;
public void Update()
{
KeyboardState keyState = Keyboard.GetState();
if (keyState.IsKeyDown(Keys.D))
{
if (!_dKeyPressed)
{
Console.WriteLine("[GameInput] D key is pressed.");
}
_dKeyPressed = true;
}
else if (_dKeyPressed)
{
Console.WriteLine("[GameInput] D key is released.");
_dKeyPressed = false;
}
}
}
}