The community looks really great and i am very happy to work with MonoGame. I am creating a game using the DesktopGL on Linux, but i am having a problem with the IsKeyDown command.
The problem is, when i check the state of any button with IsKeyDown, it returns false when the button is not pressed (as expected) but returns true and false randomly when the button is pressed.
Everything works fine on Windows, the same executable. I don’t know if it happens only on my linux, but the code is easy to reproduce:
Declare a color in the Game1 class:
Color backgroundColor;
Change the color in the Update method:
if (Keyboard.GetState().IsKeyDown(Keys.Space)) {
backgroundColor = Color.Green;
} else {
backgroundColor = Color.Red;
}
Update the color in the Draw method:
GraphicsDevice.Clear(backgroundColor);
Windows machine turns the screen to fixed green when i hold down the space button, and turns to red when i release.
Linux machine turns the screen to green for 1 second then blink from red to green even if i don’t release the space button.
What version of MonoGame are you using? If it’s a recent one, MG uses SDL for handling input and doesn’t do much itself so I’d guess it’s an SDL issue, but it seems hard to believe that a bug as big as this is in SDL. Is it possible this is an issue with your keyboard? Can you double-check that in some way?
Does the blinking happen every frame, is there some pattern to it or is it just random?
I am using MG last version 3.6;
I tried in two machines Ubuntu 16.04 and three keyboards;
It stays green for 2 seconds and starts to blink in the same pattern, then when i release, it stays blinking for 1 second, looks like buffer.
I admit that it is hard to believe it is a bug, but i don’t know what i am doing wrong then.
I’m also developing crossplatform game with Linux in mind, and one of my friends that test the game on Ubuntu has the same or at least similar issue with keyboard, but gamepad works correctly.
I have tested the game on another friend’s laptop running some Linux distribution and on Ubuntu running on Virtual Machine, and it works correctly. I also noticed that if frame rate is low on Virtual Ubuntu, input starts working weirdly.
Old versions of SDL have the function SDL_EnableKeyRepeat that we can set to 0 and disable it, in the newer version, we should be able to see if the event KEY DOWN and UP are repeat looking into the var ev.Key.Repeat (0 user key press, 1 repeat event). But i tried to check the repeat value here: https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/SDL/SDLGamePlatform.cs#L143
with no luck, it returns 0 and 1 randomly.
I tried to import the SDL function SDL_GetKeyboardState and check if the key value is pressed in the KEYUP event, and it returns 0 and 1 randomly too, as the key was pressed and released multiple times.
int numkeys;
IntPtr keyboardState = Sdl.Keyboard.GetKeyboardState(out numkeys);
byte[] keybardBuffer = new byte[numkeys];
Marshal.Copy(keyboardState, keybardBuffer, 0, numkeys);
Console.WriteLine(keybardBuffer[ev.Key.Keysym.Scancode]);
I saw a fix for something like this released recently in the SDL and people talking about the same issue in some keyboard configs.
I tried to change my keyboard settings to disable the keyrepeat, but it didn’t work as well, even if this solution was only for testing.
Then i got the lastest version of SDL and replaced the libSDL2-2.0.so.0 from the x64 bin dir by the /usr/local/lib/libSDL2-2.0.so.0.5.0 and it worked as expected. So i recommend update the MG SDL version to fix it.