test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 10 Feb 2008 17:29:23 +0000
changeset 2315 95586dfe1992
parent 2307 47f8a579cd9b
child 4560 95352c671a6e
permissions -rw-r--r--
Show integer scancode, since that's how it's defined in the header.
     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_LGUI)
    44         printf(" LGUI");
    45     if (mod & KMOD_RGUI)
    46         printf(" RGUI");
    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:  scancode %d = %s, keycode 0x%08X = %s ",
    61                pressed ? "pressed " : "released",
    62                sym->scancode,
    63                SDL_GetScancodeName(sym->scancode),
    64                sym->sym, SDL_GetKeyName(sym->sym));
    65     } else {
    66         printf("Unknown Key (scancode %d = %s) %s ",
    67                sym->scancode,
    68                SDL_GetScancodeName(sym->scancode),
    69                pressed ? "pressed" : "released");
    70     }
    71 
    72     /* Print the translated character, if one exists */
    73     if (sym->unicode) {
    74         /* Is it a control-character? */
    75         if (sym->unicode < ' ') {
    76             printf(" (^%c)", sym->unicode + '@');
    77         } else {
    78 #ifdef UNICODE
    79             printf(" (%c)", sym->unicode);
    80 #else
    81             /* This is a Latin-1 program, so only show 8-bits */
    82             if (!(sym->unicode & 0xFF00))
    83                 printf(" (%c)", sym->unicode);
    84             else
    85                 printf(" (0x%X)", sym->unicode);
    86 #endif
    87         }
    88     }
    89     print_modifiers();
    90     printf("\n");
    91 }
    92 
    93 static void
    94 PrintText(char *text)
    95 {
    96     printf("Text: %s\n", text);
    97 }
    98 
    99 int
   100 main(int argc, char *argv[])
   101 {
   102     SDL_Event event;
   103     int done;
   104     Uint32 videoflags;
   105 
   106     /* Initialize SDL */
   107     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   108         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   109         return (1);
   110     }
   111 
   112     videoflags = SDL_SWSURFACE;
   113     while (argc > 1) {
   114         --argc;
   115         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   116             videoflags |= SDL_FULLSCREEN;
   117         } else {
   118             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   119             quit(1);
   120         }
   121     }
   122 
   123     /* Set 640x480 video mode */
   124     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   125         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   126                 SDL_GetError());
   127         quit(2);
   128     }
   129 
   130     /* Watch keystrokes */
   131     done = 0;
   132     while (!done) {
   133         /* Check for events */
   134         SDL_WaitEvent(&event);
   135         switch (event.type) {
   136         case SDL_KEYDOWN:
   137             PrintKey(&event.key.keysym, 1);
   138             break;
   139         case SDL_KEYUP:
   140             PrintKey(&event.key.keysym, 0);
   141             break;
   142         case SDL_TEXTINPUT:
   143             PrintText(event.text.text);
   144             break;
   145         case SDL_MOUSEBUTTONDOWN:
   146             /* Any button press quits the app... */
   147         case SDL_QUIT:
   148             done = 1;
   149             break;
   150         default:
   151             break;
   152         }
   153     }
   154 
   155     SDL_Quit();
   156     return (0);
   157 }