src/keyboard.c

changeset 82
4c72b03ffdc0
parent 80
9581358e92b0
child 83
7d2f720daed8
     1.1 --- a/src/keyboard.c	Wed Feb 09 15:39:16 2011 +0000
     1.2 +++ b/src/keyboard.c	Wed Feb 09 15:40:05 2011 +0000
     1.3 @@ -124,8 +124,8 @@
     1.4  	{ SDLK_y,				0,	0x79 },	// Y
     1.5  	{ SDLK_z,				0,	0x7a },	// Z
     1.6  // Keycodes 7B, 7C, 7D not used
     1.7 -	{ SDLK_NUMLOCK,			0,	0x7e }		// Numlock
     1.8 -//	{ SDLK_,				1,	0x7f },	// Dlete
     1.9 +	{ SDLK_NUMLOCK,			0,	0x7e }	// Numlock
    1.10 +	{ SDLK_DELETE,			0,	0x7f },	// Dlete
    1.11  };
    1.12  
    1.13  void keyboard_init(KEYBOARD_STATE *ks)
    1.14 @@ -141,19 +141,39 @@
    1.15  
    1.16  void keyboard_event(KEYBOARD_STATE *ks, SDL_Event *ev)
    1.17  {
    1.18 -	// event handler -- handles SDL events
    1.19 +	// Ignore non-keyboard events
    1.20 +	if ((event->type != SDL_KEYDOWN) && (event->type != SDL_KEYUP)) return;
    1.21 +
    1.22 +	// scan the keymap
    1.23 +	int keyidx = 0;
    1.24 +	for (keyidx=0; keyidx < sizeof(keymap)/sizeof(keymap[0]); keyidx++) {
    1.25 +		if (keymap[keyidx].key == event->key.keysym.sym) break;
    1.26 +	}
    1.27 +
    1.28 +	switch (event->type) {
    1.29 +		// key pressed
    1.30 +		case SDL_KEYDOWN:
    1.31 +			ks->keystate[keymap[keyidx].scancode] = 1;
    1.32 +			break;
    1.33 +		// key released
    1.34 +		case SDL_KEYUP:
    1.35 +			ks->keystate[keymap[keyidx].scancode] = 1;
    1.36 +			break;
    1.37 +	}
    1.38  }
    1.39  
    1.40  void keyboard_scan(KEYBOARD_STATE *ks)
    1.41  {
    1.42  	// if buffer empty, do a keyboard scan
    1.43  	if (ks->buflen == 0) {
    1.44 +		// TODO: inject "keyboard data follows" chunk header
    1.45  		for (int i=0; i<(sizeof(ks->keystate)/sizeof(ks->keystate[0])); i++) {
    1.46  			if (ks->keystate[i]) {
    1.47  				ks->buffer[ks->writep] = i;
    1.48  				ks->writep = (ks->writep + 1) % KEYBOARD_BUFFER_SIZE;
    1.49  			}
    1.50  		}
    1.51 +		// TODO: inject "mouse data follows" chunk header and mouse movement info
    1.52  	}
    1.53  }
    1.54