test/checkkeys.c
changeset 0 74212992fb08
child 473 30466f501b77
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/checkkeys.c	Thu Apr 26 16:45:43 2001 +0000
     1.3 @@ -0,0 +1,124 @@
     1.4 +
     1.5 +/* Simple program:  Loop, watching keystrokes
     1.6 +   Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to 
     1.7 +   pump the event loop and catch keystrokes.
     1.8 +*/
     1.9 +
    1.10 +#include <stdio.h>
    1.11 +#include <stdlib.h>
    1.12 +#include <string.h>
    1.13 +
    1.14 +#include "SDL.h"
    1.15 +
    1.16 +static void print_modifiers(void)
    1.17 +{
    1.18 +	int mod;
    1.19 +	printf(" modifiers:");
    1.20 +	mod = SDL_GetModState();
    1.21 +	if(!mod) {
    1.22 +		printf(" (none)");
    1.23 +		return;
    1.24 +	}
    1.25 +	if(mod & KMOD_LSHIFT)
    1.26 +		printf(" LSHIFT");
    1.27 +	if(mod & KMOD_RSHIFT)
    1.28 +		printf(" RSHIFT");
    1.29 +	if(mod & KMOD_LCTRL)
    1.30 +		printf(" LCTRL");
    1.31 +	if(mod & KMOD_RCTRL)
    1.32 +		printf(" RCTRL");
    1.33 +	if(mod & KMOD_LALT)
    1.34 +		printf(" LALT");
    1.35 +	if(mod & KMOD_RALT)
    1.36 +		printf(" RALT");
    1.37 +	if(mod & KMOD_LMETA)
    1.38 +		printf(" LMETA");
    1.39 +	if(mod & KMOD_RMETA)
    1.40 +		printf(" RMETA");
    1.41 +	if(mod & KMOD_NUM)
    1.42 +		printf(" NUM");
    1.43 +	if(mod & KMOD_CAPS)
    1.44 +		printf(" CAPS");
    1.45 +	if(mod & KMOD_MODE)
    1.46 +		printf(" MODE");
    1.47 +}
    1.48 +
    1.49 +static void PrintKey(SDL_keysym *sym, int pressed)
    1.50 +{
    1.51 +	/* Print the keycode, name and state */
    1.52 +	if ( sym->sym ) {
    1.53 +		printf("Key %s:  %d-%s ", pressed ?  "pressed" : "released",
    1.54 +					sym->sym, SDL_GetKeyName(sym->sym));
    1.55 +	} else {
    1.56 +		printf("Unknown Key (scancode = %d) %s ", sym->scancode,
    1.57 +					pressed ?  "pressed" : "released");
    1.58 +	}
    1.59 +
    1.60 +	/* Print the translated character, if one exists */
    1.61 +	if ( sym->unicode ) {
    1.62 +		/* Is it a control-character? */
    1.63 +		if ( sym->unicode < ' ' ) {
    1.64 +			printf(" (^%c)", sym->unicode+'@');
    1.65 +		} else {
    1.66 +#ifdef UNICODE
    1.67 +			printf(" (%c)", sym->unicode);
    1.68 +#else
    1.69 +			/* This is a Latin-1 program, so only show 8-bits */
    1.70 +			if ( !(sym->unicode & 0xFF00) )
    1.71 +				printf(" (%c)", sym->unicode);
    1.72 +#endif
    1.73 +		}
    1.74 +	}
    1.75 +	print_modifiers();
    1.76 +	printf("\n");
    1.77 +}
    1.78 +
    1.79 +int main(int argc, char *argv[])
    1.80 +{
    1.81 +	SDL_Event event;
    1.82 +	int done;
    1.83 +
    1.84 +	/* Initialize SDL */
    1.85 +	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    1.86 +		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    1.87 +		exit(1);
    1.88 +	}
    1.89 +	atexit(SDL_Quit);
    1.90 +
    1.91 +	/* Set 640x480 video mode */
    1.92 +	if ( SDL_SetVideoMode(640, 480, 0, SDL_SWSURFACE) == NULL ) {
    1.93 +		fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
    1.94 +							SDL_GetError());
    1.95 +		exit(2);
    1.96 +	}
    1.97 +
    1.98 +	/* Enable UNICODE translation for keyboard input */
    1.99 +	SDL_EnableUNICODE(1);
   1.100 +
   1.101 +	/* Enable auto repeat for keyboard input */
   1.102 +	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
   1.103 +	                    SDL_DEFAULT_REPEAT_INTERVAL);
   1.104 +
   1.105 +	/* Watch keystrokes */
   1.106 +	done = 0;
   1.107 +	while ( !done ) {
   1.108 +		/* Check for events */
   1.109 +		SDL_WaitEvent(&event);
   1.110 +		switch (event.type) {
   1.111 +			case SDL_KEYDOWN:
   1.112 +				PrintKey(&event.key.keysym, 1);
   1.113 +				break;
   1.114 +			case SDL_KEYUP:
   1.115 +				PrintKey(&event.key.keysym, 0);
   1.116 +				break;
   1.117 +			case SDL_MOUSEBUTTONDOWN:
   1.118 +				/* Any button press quits the app... */
   1.119 +			case SDL_QUIT:
   1.120 +				done = 1;
   1.121 +				break;
   1.122 +			default:
   1.123 +				break;
   1.124 +		}
   1.125 +	}
   1.126 +	return(0);
   1.127 +}