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