test/checkkeys.c
author Patrice Mandin <patmandin@gmail.com>
Fri, 05 Jan 2007 19:12:50 +0000
changeset 2082 e6cd882e3ac0
parent 1895 c121d94672cb
child 2295 dbc6d1893869
permissions -rw-r--r--
Alpha sort the test list, add missing ones, remove removed ones
     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
    15 quit(int rc)
    16 {
    17     SDL_Quit();
    18     exit(rc);
    19 }
    20 
    21 static void
    22 print_modifiers(void)
    23 {
    24     int mod;
    25     printf(" modifiers:");
    26     mod = SDL_GetModState();
    27     if (!mod) {
    28         printf(" (none)");
    29         return;
    30     }
    31     if (mod & KMOD_LSHIFT)
    32         printf(" LSHIFT");
    33     if (mod & KMOD_RSHIFT)
    34         printf(" RSHIFT");
    35     if (mod & KMOD_LCTRL)
    36         printf(" LCTRL");
    37     if (mod & KMOD_RCTRL)
    38         printf(" RCTRL");
    39     if (mod & KMOD_LALT)
    40         printf(" LALT");
    41     if (mod & KMOD_RALT)
    42         printf(" RALT");
    43     if (mod & KMOD_LMETA)
    44         printf(" LMETA");
    45     if (mod & KMOD_RMETA)
    46         printf(" RMETA");
    47     if (mod & KMOD_NUM)
    48         printf(" NUM");
    49     if (mod & KMOD_CAPS)
    50         printf(" CAPS");
    51     if (mod & KMOD_MODE)
    52         printf(" MODE");
    53 }
    54 
    55 static void
    56 PrintKey(SDL_keysym * sym, int pressed)
    57 {
    58     /* Print the keycode, name and state */
    59     if (sym->sym) {
    60         printf("Key %s:  %d-%s ", pressed ? "pressed" : "released",
    61                sym->sym, SDL_GetKeyName(sym->sym));
    62     } else {
    63         printf("Unknown Key (scancode = %d) %s ", sym->scancode,
    64                pressed ? "pressed" : "released");
    65     }
    66 
    67     /* Print the translated character, if one exists */
    68     if (sym->unicode) {
    69         /* Is it a control-character? */
    70         if (sym->unicode < ' ') {
    71             printf(" (^%c)", sym->unicode + '@');
    72         } else {
    73 #ifdef UNICODE
    74             printf(" (%c)", sym->unicode);
    75 #else
    76             /* This is a Latin-1 program, so only show 8-bits */
    77             if (!(sym->unicode & 0xFF00))
    78                 printf(" (%c)", sym->unicode);
    79             else
    80                 printf(" (0x%X)", sym->unicode);
    81 #endif
    82         }
    83     }
    84     print_modifiers();
    85     printf("\n");
    86 }
    87 
    88 int
    89 main(int argc, char *argv[])
    90 {
    91     SDL_Event event;
    92     int done;
    93     Uint32 videoflags;
    94 
    95     /* Initialize SDL */
    96     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    97         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
    98         return (1);
    99     }
   100 
   101     videoflags = SDL_SWSURFACE;
   102     while (argc > 1) {
   103         --argc;
   104         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   105             videoflags |= SDL_FULLSCREEN;
   106         } else {
   107             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   108             quit(1);
   109         }
   110     }
   111 
   112     /* Set 640x480 video mode */
   113     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   114         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   115                 SDL_GetError());
   116         quit(2);
   117     }
   118 
   119     /* Enable UNICODE translation for keyboard input */
   120     SDL_EnableUNICODE(1);
   121 
   122     /* Enable auto repeat for keyboard input */
   123     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
   124                         SDL_DEFAULT_REPEAT_INTERVAL);
   125 
   126     /* Watch keystrokes */
   127     done = 0;
   128     while (!done) {
   129         /* Check for events */
   130         SDL_WaitEvent(&event);
   131         switch (event.type) {
   132         case SDL_KEYDOWN:
   133             PrintKey(&event.key.keysym, 1);
   134             break;
   135         case SDL_KEYUP:
   136             PrintKey(&event.key.keysym, 0);
   137             break;
   138         case SDL_MOUSEBUTTONDOWN:
   139             /* Any button press quits the app... */
   140         case SDL_QUIT:
   141             done = 1;
   142             break;
   143         default:
   144             break;
   145         }
   146     }
   147 
   148     SDL_Quit();
   149     return (0);
   150 }