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:

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


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

Any ideas?


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:
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);

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 from the x64 bin dir by the /usr/local/lib/ and it worked as expected. So i recommend update the MG SDL version to fix it.


1 Like

Link for the SDL fix:

How to install the fixed SDL:
hg clone SDL
cd SDL
mkdir build
cd build
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:

It fixed the issue in my linux.


1 Like