src/keyboard.c

changeset 90
934ae2efdd01
parent 85
9883eb142f70
parent 86
ebce87d87808
child 91
781c15e60012
     1.1 --- a/src/keyboard.c	Wed Feb 09 17:11:48 2011 +0000
     1.2 +++ b/src/keyboard.c	Wed Feb 09 22:05:42 2011 +0000
     1.3 @@ -128,6 +128,29 @@
     1.4  	{ SDLK_DELETE,			0,	0x7f }	// Dlete
     1.5  };
     1.6  
     1.7 +/**
     1.8 + * List of special key codes
     1.9 + */
    1.10 +enum {
    1.11 +	KEY_ALL_UP				= 0x40,		///< All keys up
    1.12 +	KEY_LIST_END			= 0x80,		///< End of key code list
    1.13 +	KEY_BEGIN_MOUSE			= 0xCF,		///< Mouse data follows
    1.14 +	KEY_BEGIN_KEYBOARD		= 0xDF,		///< Keyboard data follows
    1.15 +};
    1.16 +
    1.17 +/**
    1.18 + * List of keyboard commands
    1.19 + */
    1.20 +enum {
    1.21 +	KEY_CMD_RESET			= 0x92,		///< Reset keyboard
    1.22 +	KEY_CMD_CAPSLED_OFF		= 0xB1,		///< Caps Lock LED off--CHECK!
    1.23 +	KEY_CMD_CAPSLED_ON		= 0xB0,		///< Caps Lock LED on --CHECK!
    1.24 +	KEY_CMD_NUMLED_OFF		= 0xA1,		///< Num Lock LED off --CHECK!
    1.25 +	KEY_CMD_NUMLED_ON		= 0xA0,		///< Num Lock LED on  --CHECK!
    1.26 +	KEY_CMD_MOUSE_ENABLE	= 0xD0,		///< Enable mouse
    1.27 +	KEY_CMD_MOUSE_DISABLE	= 0xD1		///< Disable mouse
    1.28 +};
    1.29 +
    1.30  void keyboard_init(KEYBOARD_STATE *ks)
    1.31  {
    1.32  	// Set all key states to "not pressed"
    1.33 @@ -141,29 +164,40 @@
    1.34  
    1.35  void keyboard_event(KEYBOARD_STATE *ks, SDL_Event *ev)
    1.36  {
    1.37 -	// Ignore non-keyboard events
    1.38 -	if ((ev->type != SDL_KEYDOWN) && (ev->type != SDL_KEYUP)) return;
    1.39 +	int v = 0;
    1.40 +	switch (ev->type) {
    1.41 +		case SDL_KEYDOWN:
    1.42 +			// Key down (pressed)
    1.43 +			v = 1;
    1.44 +			break;
    1.45 +		case SDL_KEYUP:
    1.46 +			// Key up (released)
    1.47 +			v = 0;
    1.48 +			break;
    1.49 +		default:
    1.50 +			// Not a keyboard event
    1.51 +			return;
    1.52 +	}
    1.53  
    1.54  	// scan the keymap
    1.55 -	int keyidx = 0;
    1.56 -	bool found = false;
    1.57 -	for (keyidx=0; keyidx < sizeof(keymap)/sizeof(keymap[0]); keyidx++) {
    1.58 -		if (keymap[keyidx].key == ev->key.keysym.sym) {
    1.59 -			found = true;
    1.60 -			break;
    1.61 +	for (int i=0; i < sizeof(keymap)/sizeof(keymap[0]); i++) {
    1.62 +		if (keymap[i].key == ev->key.keysym.sym) {
    1.63 +			// Keycode match. Is this an Extended Map key?
    1.64 +			if (keymap[i].extended) {
    1.65 +				// Yes -- need ALT set when pressing the key for this to be a match
    1.66 +				if (ev->key.keysym.mod & KMOD_ALT) {
    1.67 +					ks->keystate[keymap[i].scancode] = v;
    1.68 +					break;
    1.69 +				}
    1.70 +			} else {
    1.71 +				// Standard Map key. ALT must NOT be pressed for this to be a match
    1.72 +				if (!(ev->key.keysym.mod & KMOD_ALT)) {
    1.73 +					ks->keystate[keymap[i].scancode] = v;
    1.74 +					break;
    1.75 +				}
    1.76 +			}
    1.77  		}
    1.78  	}
    1.79 -
    1.80 -	switch (ev->type) {
    1.81 -		// key pressed
    1.82 -		case SDL_KEYDOWN:
    1.83 -			ks->keystate[keymap[keyidx].scancode] = 1;
    1.84 -			break;
    1.85 -		// key released
    1.86 -		case SDL_KEYUP:
    1.87 -			ks->keystate[keymap[keyidx].scancode] = 0;
    1.88 -			break;
    1.89 -	}
    1.90  }
    1.91  
    1.92  void keyboard_scan(KEYBOARD_STATE *ks)