IsKeyDown is not working on linux

Hi buddies, it is my first post here.

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:

  1. Declare a color in the Game1 class:
    Color backgroundColor;

  2. Change the color in the Update method:
    if (Keyboard.GetState().IsKeyDown(Keys.Space)) {
    backgroundColor = Color.Green;
    } else {
    backgroundColor = Color.Red;
    }

  3. 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.

I am sorry if i missed something.

Thank you all!

1 Like

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?

Hi @Jjagg, thanks for your reply.

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.

The code and the exe is here if someone wants to try it http://luizpestana.com/files/Game1.tar.gz

Thanks

Tried now with the development build 3.7.0.444 and i got the same behavior.

Any ideas?

Thanks!

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.

I found the solution, my workaround:

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.

Thanks!

1 Like

Link for the SDL fix: https://hg.libsdl.org/SDL/rev/b48d8a98e261

How to install the fixed SDL:
hg clone https://hg.libsdl.org/SDL SDL
cd SDL
mkdir build
cd build
…/configure
make
sudo make install

1 Like

Would be great to have the SDL updated in the MonoGame repository as they released a newer version (2.0.6).

Great! For information like that IMO it’s better to open an issue on the GitHub repo so more contributors see it.

Hi @Jjagg, yes, i asked @harry-cpp and he kindly compiled the new SDL and created a PR: https://github.com/MonoGame/MonoGame.Dependencies/pull/114

It fixed the issue in my linux.

Thanks!

1 Like