test/checkkeys.c
author Ryan C. Gordon <icculus@icculus.org>
Thu, 29 Sep 2005 09:43:00 +0000
changeset 1152 51a8702d8ecd
parent 1151 be9c9c8f6d53
child 1253 7c7ddaf195bf
permissions -rw-r--r--
Updates to PocketPC (WinCE) support, thanks to Dmitry Yakimov at
activekitten.com.
     1 
     2 /* Simple program:  Loop, watching keystrokes
     3    Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to 
     4    pump the event loop and catch keystrokes.
     5 */
     6 
     7 #include <stdio.h>
     8 #include <stdlib.h>
     9 #include <string.h>
    10 
    11 #include "SDL.h"
    12 
    13 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    14 static void quit(int rc)
    15 {
    16 	SDL_Quit();
    17 	exit(rc);
    18 }
    19 
    20 static void print_modifiers(void)
    21 {
    22 	int mod;
    23 	printf(" modifiers:");
    24 	mod = SDL_GetModState();
    25 	if(!mod) {
    26 		printf(" (none)");
    27 		return;
    28 	}
    29 	if(mod & KMOD_LSHIFT)
    30 		printf(" LSHIFT");
    31 	if(mod & KMOD_RSHIFT)
    32 		printf(" RSHIFT");
    33 	if(mod & KMOD_LCTRL)
    34 		printf(" LCTRL");
    35 	if(mod & KMOD_RCTRL)
    36 		printf(" RCTRL");
    37 	if(mod & KMOD_LALT)
    38 		printf(" LALT");
    39 	if(mod & KMOD_RALT)
    40 		printf(" RALT");
    41 	if(mod & KMOD_LMETA)
    42 		printf(" LMETA");
    43 	if(mod & KMOD_RMETA)
    44 		printf(" RMETA");
    45 	if(mod & KMOD_NUM)
    46 		printf(" NUM");
    47 	if(mod & KMOD_CAPS)
    48 		printf(" CAPS");
    49 	if(mod & KMOD_MODE)
    50 		printf(" MODE");
    51 }
    52 
    53 static void PrintKey(SDL_keysym *sym, int pressed)
    54 {
    55 	/* Print the keycode, name and state */
    56 	if ( sym->sym ) {
    57 		printf("Key %s:  %d-%s ", pressed ?  "pressed" : "released",
    58 					sym->sym, SDL_GetKeyName(sym->sym));
    59 	} else {
    60 		printf("Unknown Key (scancode = %d) %s ", sym->scancode,
    61 					pressed ?  "pressed" : "released");
    62 	}
    63 
    64 	/* Print the translated character, if one exists */
    65 	if ( sym->unicode ) {
    66 		/* Is it a control-character? */
    67 		if ( sym->unicode < ' ' ) {
    68 			printf(" (^%c)", sym->unicode+'@');
    69 		} else {
    70 #ifdef UNICODE
    71 			printf(" (%c)", sym->unicode);
    72 #else
    73 			/* This is a Latin-1 program, so only show 8-bits */
    74 			if ( !(sym->unicode & 0xFF00) )
    75 				printf(" (%c)", sym->unicode);
    76 #endif
    77 		}
    78 	}
    79 	print_modifiers();
    80 	printf("\n");
    81 }
    82 
    83 int main(int argc, char *argv[])
    84 {
    85 	SDL_Event event;
    86 	int done;
    87 	Uint32 videoflags;
    88 
    89 	/* Initialize SDL */
    90 	if ( SDL_Init(SDL_INIT_VIDEO) < 0 ) {
    91 		fprintf(stderr, "Couldn't initialize SDL: %s\n",SDL_GetError());
    92 		return(1);
    93 	}
    94 
    95 	videoflags = SDL_SWSURFACE;
    96 	while( argc > 1 ) {
    97 		--argc;
    98 		if ( argv[argc] && !strcmp(argv[argc], "-fullscreen") ) {
    99 			videoflags |= SDL_FULLSCREEN;
   100 		} else {
   101 			fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   102 			quit(1);
   103 		}
   104 	}
   105 
   106 	/* Set 640x480 video mode */
   107 	if ( SDL_SetVideoMode(640, 480, 0, videoflags) == NULL ) {
   108 		fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   109 							SDL_GetError());
   110 		quit(2);
   111 	}
   112 
   113 	/* Enable UNICODE translation for keyboard input */
   114 	SDL_EnableUNICODE(1);
   115 
   116 	/* Enable auto repeat for keyboard input */
   117 	SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
   118 	                    SDL_DEFAULT_REPEAT_INTERVAL);
   119 
   120 	/* Watch keystrokes */
   121 	done = 0;
   122 	while ( !done ) {
   123 		/* Check for events */
   124 		SDL_WaitEvent(&event);
   125 		switch (event.type) {
   126 			case SDL_KEYDOWN:
   127 				PrintKey(&event.key.keysym, 1);
   128 				break;
   129 			case SDL_KEYUP:
   130 				PrintKey(&event.key.keysym, 0);
   131 				break;
   132 			case SDL_MOUSEBUTTONDOWN:
   133 				/* Any button press quits the app... */
   134 			case SDL_QUIT:
   135 				done = 1;
   136 				break;
   137 			default:
   138 				break;
   139 		}
   140 	}
   141 
   142 	SDL_Quit();
   143 	return(0);
   144 }