From 386b48ee2ed1ec17b06538a8c8c3762727377aec Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Wed, 7 Nov 2012 11:13:28 -0800 Subject: [PATCH] Reset the keyboard when we lose input focus --- src/events/SDL_keyboard.c | 10 +++++++++- src/video/x11/SDL_x11events.c | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/events/SDL_keyboard.c b/src/events/SDL_keyboard.c index 366e0184e..e58f8ba36 100644 --- a/src/events/SDL_keyboard.c +++ b/src/events/SDL_keyboard.c @@ -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_ResetKeyboard(void) 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 @@ SDL_SetKeyboardFocus(SDL_Window * window) video->StartTextInput(video); } } + } else { + /* We won't get anymore keyboard messages, so reset keyboard state */ + SDL_ResetKeyboard(); } } @@ -648,7 +656,7 @@ SDL_SendKeyboardKey(Uint8 state, SDL_Scancode scancode) 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 diff --git a/src/video/x11/SDL_x11events.c b/src/video/x11/SDL_x11events.c index 51f342629..480bb3149 100644 --- a/src/video/x11/SDL_x11events.c +++ b/src/video/x11/SDL_x11events.c @@ -258,6 +258,13 @@ X11_DispatchEvent(_THIS) #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; }