test/checkkeys.c

/* [<][>]
[^][v][top][bottom][index][help] */

FUNCTIONS

This source file includes following functions.
  1. print_modifiers
  2. PrintKey
  3. main

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

/* [<][>][^][v][top][bottom][index][help] */