Reset the keyboard when we lose input focus
authorSam Lantinga <slouken@libsdl.org>
Wed, 07 Nov 2012 11:13:28 -0800
changeset 6661 cd8befc0a969
parent 6660 4fa0723e6874
child 6662 698c2e533a7e
Reset the keyboard when we lose input focus
src/events/SDL_keyboard.c
src/video/x11/SDL_x11events.c
--- a/src/events/SDL_keyboard.c	Wed Nov 07 08:49:59 2012 -0800
+++ b/src/events/SDL_keyboard.c	Wed Nov 07 11:13:28 2012 -0800
@@ -28,6 +28,8 @@
 #include "../video/SDL_sysvideo.h"
 
 
+/*#define DEBUG_KEYBOARD*/
+
 /* Global keyboard information */
 
 typedef struct SDL_Keyboard SDL_Keyboard;
@@ -563,6 +565,9 @@
     SDL_Keyboard *keyboard = &SDL_keyboard;
     SDL_Scancode scancode;
 
+#ifdef DEBUG_KEYBOARD
+    printf("Resetting keyboard\n");
+#endif
     for (scancode = 0; scancode < SDL_NUM_SCANCODES; ++scancode) {
         if (keyboard->keystate[scancode] == SDL_PRESSED) {
             SDL_SendKeyboardKey(SDL_RELEASED, scancode);
@@ -633,6 +638,9 @@
                 video->StartTextInput(video);
             }
         }
+    } else {
+        /* We won't get anymore keyboard messages, so reset keyboard state */
+        SDL_ResetKeyboard();
     }
 }
 
@@ -648,7 +656,7 @@
     if (!scancode) {
         return 0;
     }
-#if 0
+#ifdef DEBUG_KEYBOARD
     printf("The '%s' key has been %s\n", SDL_GetScancodeName(scancode),
            state == SDL_PRESSED ? "pressed" : "released");
 #endif
--- a/src/video/x11/SDL_x11events.c	Wed Nov 07 08:49:59 2012 -0800
+++ b/src/video/x11/SDL_x11events.c	Wed Nov 07 11:13:28 2012 -0800
@@ -258,6 +258,13 @@
 #ifdef DEBUG_XEVENTS
             printf("window %p: FocusIn!\n", data);
 #endif
+            if (data->pending_focus == PENDING_FOCUS_OUT &&
+                data->window == SDL_GetKeyboardFocus()) {
+                /* We want to reset the keyboard here, because we may have
+                   missed keyboard messages after our previous FocusOut.
+                 */
+                SDL_ResetKeyboard();
+            }
             data->pending_focus = PENDING_FOCUS_IN;
             data->pending_focus_time = SDL_GetTicks() + PENDING_FOCUS_IN_TIME;
         }