The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDL_GetKeyboardState bug ? Key stuck after window moved
sgrsgsrg


Joined: 06 Sep 2016
Posts: 18
I noticed a strange thing and wondered if it's a SDL bug : when my app have the focus, I press a key, then while keeping this key pressend, I move the window. Now, the state for this key is always stuck to 1, and I didnt found a way to stop it.
My event code isn't special, I just do :

int keystate = SDL_GetKeyboardState(NULL);

at the game initialization, then i retrieve the key state with keystate[code]...
sgrsgsrg


Joined: 06 Sep 2016
Posts: 18
the bug is 100% reproductible, when you keep pressing a key while moving the window, release it BEFORE you release the mouse button
SDL_GetKeyboardState bug ? Key stuck after window moved
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
What operating system?

On Mon, Sep 12, 2016 at 2:24 PM, sgrsgsrg wrote:
Quote:
the bug is 100% reproductible, when you keep pressing a key while moving the window, release it BEFORE you release the mouse button


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

sgrsgsrg


Joined: 06 Sep 2016
Posts: 18
Windows 10.

SDL2.0.4, compiled under Visual Studio 2013

I currently tested only with the left & right keys. (SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT)


To reproduce the problem :
- press a key
- while keeping this key pressed, move the window with your mouse
- release the key
- release the mouse click to stop moving the window
-> the key state for this key will be stuck forever


If you release the key after you stopped moving the window, the problem does not occurs
sgrsgsrg


Joined: 06 Sep 2016
Posts: 18
Some news.

The only way i found to reset a stuck key is to make the window lose then gain its focus.

This code will force focus change if a key was pressed while the window was moved, thus correcting the stucked key glitch

Code:

while (SDL_PollEvent(&e)) {
   switch (e.type){
      if (e.window.event == SDL_WINDOWEVENT_MOVED) {
         // check if a key was already pressed
         int sdl_bug = 0;
         for (int i = 0; i < 7; i++){ // the 7 keyboard keys i use in my game
            if (keys[i]){
               sdl_bug = 1;
            }
         }
         if (sdl_bug){ // hack
            SDL_MinimizeWindow(window);
            SDL_RestoreWindow(window);
         }
      }
   }
}


Hacks are ugly, it'd be nice if some investigation could be done