Date: Sat, 15 Jan 2005 02:01:51 +0000 (UTC)
authorSam Lantinga <slouken@libsdl.org>
Sun, 29 Jan 2006 07:57:13 +0000
changeset 1282217f5d5a49e5
parent 1281 644b39bf7253
child 1283 f214b6fae45a
Date: Sat, 15 Jan 2005 02:01:51 +0000 (UTC)
From: jimrandomh
Subject: [SDL] Re: Modifier keys pressed during initialization stick

I wrote a simple test program which initializes SDL, prints the SDL
version number, then prints any keydown and keyup events with their
modifiers. (Source code below). Compilation was done using Visual
Studio 6, release mode.

My test sequence was:
Start a command prompt. Type the name of the test program.
shift down
enter down (program starts)
Wait for window to appear
enter up
shift up
spacebar down
spacebar up

Under Windows 98, the output was correct:
SDL 1.2.8
left shift down
shift-return down
shift-return up
left shift up
space down
space up

Under Windows 2000 and under Windows XP, the output was:
SDL 1.2.8
shift-space down
shift-space up

Since shift was not held at the time space was pressed, this is
incorrect. Similar results were observed with launching in different
ways (including double-clicking in Windows Explorer), so it does not
depend on the launching terminal.
src/events/SDL_keyboard.c
src/video/wincommon/SDL_sysevents.c
src/video/windib/SDL_dibevents.c
     1.1 --- a/src/events/SDL_keyboard.c	Sun Jan 29 06:40:13 2006 +0000
     1.2 +++ b/src/events/SDL_keyboard.c	Sun Jan 29 07:57:13 2006 +0000
     1.3 @@ -506,6 +506,9 @@
     1.4  
     1.5  	/* Drop events that don't change state */
     1.6  	if ( SDL_KeyState[keysym->sym] == state ) {
     1.7 +#if 0
     1.8 +printf("Event didn't change state - dropped!\n");
     1.9 +#endif
    1.10  		return(0);
    1.11  	}
    1.12  
     2.1 --- a/src/video/wincommon/SDL_sysevents.c	Sun Jan 29 06:40:13 2006 +0000
     2.2 +++ b/src/video/wincommon/SDL_sysevents.c	Sun Jan 29 07:57:13 2006 +0000
     2.3 @@ -214,21 +214,27 @@
     2.4  	if ( GetKeyboardState(keyboard) ) {
     2.5  		if ( keyboard[VK_LSHIFT] & 0x80) {
     2.6  			state |= KMOD_LSHIFT;
     2.7 +			kstate[SDLK_LSHIFT] = SDL_PRESSED;
     2.8  		}
     2.9  		if ( keyboard[VK_RSHIFT] & 0x80) {
    2.10  			state |= KMOD_RSHIFT;
    2.11 +			kstate[SDLK_RSHIFT] = SDL_PRESSED;
    2.12  		}
    2.13  		if ( keyboard[VK_LCONTROL] & 0x80) {
    2.14  			state |= KMOD_LCTRL;
    2.15 +			kstate[SDLK_LCTRL] = SDL_PRESSED;
    2.16  		}
    2.17  		if ( keyboard[VK_RCONTROL] & 0x80) {
    2.18  			state |= KMOD_RCTRL;
    2.19 +			kstate[SDLK_RCTRL] = SDL_PRESSED;
    2.20  		}
    2.21  		if ( keyboard[VK_LMENU] & 0x80) {
    2.22  			state |= KMOD_LALT;
    2.23 +			kstate[SDLK_LALT] = SDL_PRESSED;
    2.24  		}
    2.25  		if ( keyboard[VK_RMENU] & 0x80) {
    2.26  			state |= KMOD_RALT;
    2.27 +			kstate[SDLK_RALT] = SDL_PRESSED;
    2.28  		}
    2.29  		if ( keyboard[VK_NUMLOCK] & 0x01) {
    2.30  			state |= KMOD_NUM;
     3.1 --- a/src/video/windib/SDL_dibevents.c	Sun Jan 29 06:40:13 2006 +0000
     3.2 +++ b/src/video/windib/SDL_dibevents.c	Sun Jan 29 07:57:13 2006 +0000
     3.3 @@ -49,7 +49,6 @@
     3.4  /* The translation table from a Microsoft VK keysym to a SDL keysym */
     3.5  static SDLKey VK_keymap[SDLK_LAST];
     3.6  static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed);
     3.7 -static BOOL prev_shiftstates[2];
     3.8  
     3.9  /* Masks for processing the windows KEYDOWN and KEYUP messages */
    3.10  #define REPEATED_KEYMASK	(1<<30)
    3.11 @@ -117,14 +116,16 @@
    3.12  					break;
    3.13  				case VK_SHIFT:
    3.14  					/* EXTENDED trick doesn't work here */
    3.15 -					if (!prev_shiftstates[0] && (GetKeyState(VK_LSHIFT) & 0x8000)) {
    3.16 +					{
    3.17 +					Uint8 *state = SDL_GetKeyState(NULL);
    3.18 +					if (state[SDLK_LSHIFT] == SDL_RELEASED && (GetKeyState(VK_LSHIFT) & 0x8000)) {
    3.19  						wParam = VK_LSHIFT;
    3.20 -						prev_shiftstates[0] = TRUE;
    3.21 -					} else if (!prev_shiftstates[1] && (GetKeyState(VK_RSHIFT) & 0x8000)) {
    3.22 +					} else if (state[SDLK_RSHIFT] == SDL_RELEASED && (GetKeyState(VK_RSHIFT) & 0x8000)) {
    3.23  						wParam = VK_RSHIFT;
    3.24 -						prev_shiftstates[1] = TRUE;
    3.25  					} else {
    3.26 -						/* Huh? */
    3.27 +						/* Probably a key repeat */
    3.28 +						return(0);
    3.29 +					}
    3.30  					}
    3.31  					break;
    3.32  				case VK_MENU:
    3.33 @@ -178,14 +179,16 @@
    3.34  					break;
    3.35  				case VK_SHIFT:
    3.36  					/* EXTENDED trick doesn't work here */
    3.37 -					if (prev_shiftstates[0] && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
    3.38 +					{
    3.39 +					Uint8 *state = SDL_GetKeyState(NULL);
    3.40 +					if (state[SDLK_LSHIFT] == SDL_PRESSED && !(GetKeyState(VK_LSHIFT) & 0x8000)) {
    3.41  						wParam = VK_LSHIFT;
    3.42 -						prev_shiftstates[0] = FALSE;
    3.43 -					} else if (prev_shiftstates[1] && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
    3.44 +					} else if (state[SDLK_RSHIFT] == SDL_PRESSED && !(GetKeyState(VK_RSHIFT) & 0x8000)) {
    3.45  						wParam = VK_RSHIFT;
    3.46 -						prev_shiftstates[1] = FALSE;
    3.47  					} else {
    3.48 -						/* Huh? */
    3.49 +						/* Probably a key repeat */
    3.50 +						return(0);
    3.51 +					}
    3.52  					}
    3.53  					break;
    3.54  				case VK_MENU:
    3.55 @@ -372,9 +375,6 @@
    3.56  	VK_keymap[VK_SNAPSHOT] = SDLK_PRINT;
    3.57  	VK_keymap[VK_CANCEL] = SDLK_BREAK;
    3.58  	VK_keymap[VK_APPS] = SDLK_MENU;
    3.59 -
    3.60 -	prev_shiftstates[0] = FALSE;
    3.61 -	prev_shiftstates[1] = FALSE;
    3.62  }
    3.63  
    3.64  static SDL_keysym *TranslateKey(UINT vkey, UINT scancode, SDL_keysym *keysym, int pressed)