x11: Don't send duplicate events when reconciling the keyboard state
authorBastien Bouclet <bastien.bouclet@gmail.com>
Sat, 22 Apr 2017 19:53:52 +0200
changeset 110551f2cb42aa5d3
parent 11054 0092cb66f341
child 11057 2442e955634b
x11: Don't send duplicate events when reconciling the keyboard state

Failing to check if a key was known to be pressed by SDL was causing
SDL_SendKeyboardKey to send duplicate key pressed events with the repeat
property set to true.

Fixes Bugzilla #3637.
src/video/x11/SDL_x11events.c
     1.1 --- a/src/video/x11/SDL_x11events.c	Mon May 29 18:24:06 2017 -0400
     1.2 +++ b/src/video/x11/SDL_x11events.c	Sat Apr 22 19:53:52 2017 +0200
     1.3 @@ -348,6 +348,7 @@
     1.4      Window junk_window;
     1.5      int x, y;
     1.6      unsigned int mask;
     1.7 +    const Uint8 *keyboardState;
     1.8  
     1.9      X11_XQueryKeymap(display, keys);
    1.10  
    1.11 @@ -357,11 +358,16 @@
    1.12          SDL_ToggleModState(KMOD_NUM, (mask & X11_GetNumLockModifierMask(_this)) != 0);
    1.13      }
    1.14  
    1.15 +    keyboardState = SDL_GetKeyboardState(0);
    1.16      for (keycode = 0; keycode < 256; ++keycode) {
    1.17 -        if (keys[keycode / 8] & (1 << (keycode % 8))) {
    1.18 -            SDL_SendKeyboardKey(SDL_PRESSED, viddata->key_layout[keycode]);
    1.19 -        } else {
    1.20 -            SDL_SendKeyboardKey(SDL_RELEASED, viddata->key_layout[keycode]);
    1.21 +        SDL_Scancode scancode = viddata->key_layout[keycode];
    1.22 +        SDL_bool x11KeyPressed = (keys[keycode / 8] & (1 << (keycode % 8))) != 0;
    1.23 +        SDL_bool sdlKeyPressed = keyboardState[scancode] == SDL_PRESSED;
    1.24 +
    1.25 +        if (x11KeyPressed && !sdlKeyPressed) {
    1.26 +            SDL_SendKeyboardKey(SDL_PRESSED, scancode);
    1.27 +        } else if (!x11KeyPressed && sdlKeyPressed) {
    1.28 +            SDL_SendKeyboardKey(SDL_RELEASED, scancode);
    1.29          }
    1.30      }
    1.31  }