Date: Tue, 05 Feb 2008 01:41:08 -0500 SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Tue, 26 Feb 2008 10:50:28 +0000
branchSDL-1.2
changeset 4138b1fd24d62e55
parent 4137 be12463f31ea
child 4139 568c9b3c0167
Date: Tue, 05 Feb 2008 01:41:08 -0500
From: Mike Miscevic
Subject: SDL and capslock/numlock

Find attached a patch against SDL-1.2.13 for check of SDL_NO_LOCK_KEYS
environment variable. This differs slightly from other patches I've seen
in that it has 3 modes:

Disable CAPS-LOCK and NUM-LOCK supression of down+up key events,
suitable for games where the player needs these keys to do more than
just toggle. A value of 1 will effect both CAPS-LOCK and NUM-LOCK. A
value of 2 will effect only CAPS-LOCK. A value of 3 will effect only
NUM-LOCK. All other values have no effect.

This works for me and has been tested on:
- Fedora 8 64-bit
- SRCRPM SDL-1.2.13-1.fc8.src.rpm
- Emeny Territory Quake Wars (ETQW), native 32-bit commercial game

--Mike Miscevic
docs/html/sdlenvvars.html
src/events/SDL_keyboard.c
     1.1 --- a/docs/html/sdlenvvars.html	Mon Feb 04 17:22:56 2008 +0000
     1.2 +++ b/docs/html/sdlenvvars.html	Tue Feb 26 10:50:28 2008 +0000
     1.3 @@ -670,6 +670,21 @@
     1.4  ><P
     1.5  >If set, the linux fbcon driver will not use a mouse at all.</P
     1.6  ></DD
     1.7 +><DT
     1.8 +><TT
     1.9 +CLASS="LITERAL"
    1.10 +>SDL_NO_LOCK_KEYS</TT
    1.11 +></DT
    1.12 +><DD
    1.13 +><P
    1.14 +>Disable CAPS-LOCK and NUM-LOCK suppression of down+up key events,
    1.15 +suitable for games where the player needs these keys to do more than just toggle.
    1.16 +A value of 1 will effect both CAPS-LOCK and NUM-LOCK.
    1.17 +A value of 2 will effect only CAPS-LOCK.
    1.18 +A value of 3 will effect only NUM-LOCK.
    1.19 +All other values have no effect.
    1.20 +</P
    1.21 +></DD
    1.22  ></DL
    1.23  ></DIV
    1.24  ></DIV
     2.1 --- a/src/events/SDL_keyboard.c	Mon Feb 04 17:22:56 2008 +0000
     2.2 +++ b/src/events/SDL_keyboard.c	Tue Feb 26 10:50:28 2008 +0000
     2.3 @@ -48,9 +48,16 @@
     2.4  	SDL_Event evt;    /* the event we are supposed to repeat */
     2.5  } SDL_KeyRepeat;
     2.6  
     2.7 +/* Global no-lock-keys support */
     2.8 +static Uint8 SDL_NoLockKeys;
     2.9 +
    2.10 +#define SDL_NLK_CAPS 0x01
    2.11 +#define SDL_NLK_NUM  0x02
    2.12 +
    2.13  /* Public functions */
    2.14  int SDL_KeyboardInit(void)
    2.15  {
    2.16 +	const char* env;
    2.17  	SDL_VideoDevice *video = current_video;
    2.18  	SDL_VideoDevice *this  = current_video;
    2.19  
    2.20 @@ -65,6 +72,25 @@
    2.21  
    2.22  	SDL_EnableKeyRepeat(0, 0);
    2.23  
    2.24 +	/* Allow environment override to disable special lock-key behavior */
    2.25 +	env = getenv("SDL_NO_LOCK_KEYS");
    2.26 +	SDL_NoLockKeys = 0;
    2.27 +	if (env) {
    2.28 +		switch (SDL_atoi(env)) {
    2.29 +			case 1:
    2.30 +				SDL_NoLockKeys = SDL_NLK_CAPS | SDL_NLK_NUM;
    2.31 +				break;
    2.32 +			case 2:
    2.33 +				SDL_NoLockKeys = SDL_NLK_CAPS;
    2.34 +				break;
    2.35 +			case 3:
    2.36 +				SDL_NoLockKeys = SDL_NLK_NUM;
    2.37 +				break;
    2.38 +			default:
    2.39 +				break;
    2.40 +		}
    2.41 +	}
    2.42 +
    2.43  	/* Fill in the blanks in keynames */
    2.44  	keynames[SDLK_BACKSPACE] = "backspace";
    2.45  	keynames[SDLK_TAB] = "tab";
    2.46 @@ -394,12 +420,16 @@
    2.47  				break;
    2.48  			case SDLK_NUMLOCK:
    2.49  				modstate ^= KMOD_NUM;
    2.50 +				if ( SDL_NoLockKeys & SDL_NLK_NUM )
    2.51 +					break;
    2.52  				if ( ! (modstate&KMOD_NUM) )
    2.53  					state = SDL_RELEASED;
    2.54  				keysym->mod = (SDLMod)modstate;
    2.55  				break;
    2.56  			case SDLK_CAPSLOCK:
    2.57  				modstate ^= KMOD_CAPS;
    2.58 +				if ( SDL_NoLockKeys & SDL_NLK_CAPS )
    2.59 +					break;
    2.60  				if ( ! (modstate&KMOD_CAPS) )
    2.61  					state = SDL_RELEASED;
    2.62  				keysym->mod = (SDLMod)modstate;
    2.63 @@ -440,7 +470,13 @@
    2.64  			case SDLK_UNKNOWN:
    2.65  				break;
    2.66  			case SDLK_NUMLOCK:
    2.67 +				if ( SDL_NoLockKeys & SDL_NLK_NUM )
    2.68 +					break;
    2.69 +				/* Only send keydown events */
    2.70 +				return(0);
    2.71  			case SDLK_CAPSLOCK:
    2.72 +				if ( SDL_NoLockKeys & SDL_NLK_CAPS )
    2.73 +					break;
    2.74  				/* Only send keydown events */
    2.75  				return(0);
    2.76  			case SDLK_LCTRL: