test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 20 Jul 2010 23:25:24 -0700
changeset 4560 95352c671a6e
parent 2315 95586dfe1992
child 5081 25d4feb7c127
permissions -rw-r--r--
Added support for keyboard repeat (only tested on Windows so far)
     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, SDL_bool pressed, SDL_bool repeat)
    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     if (repeat) {
    91         printf(" (repeat)");
    92     }
    93     printf("\n");
    94 }
    95 
    96 static void
    97 PrintText(char *text)
    98 {
    99     printf("Text: %s\n", text);
   100 }
   101 
   102 int
   103 main(int argc, char *argv[])
   104 {
   105     SDL_Event event;
   106     int done;
   107     Uint32 videoflags;
   108 
   109     /* Initialize SDL */
   110     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   111         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   112         return (1);
   113     }
   114 
   115     videoflags = SDL_SWSURFACE;
   116     while (argc > 1) {
   117         --argc;
   118         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   119             videoflags |= SDL_FULLSCREEN;
   120         } else {
   121             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   122             quit(1);
   123         }
   124     }
   125 
   126     /* Set 640x480 video mode */
   127     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   128         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   129                 SDL_GetError());
   130         quit(2);
   131     }
   132 
   133     /* Watch keystrokes */
   134     done = 0;
   135     while (!done) {
   136         /* Check for events */
   137         SDL_WaitEvent(&event);
   138         switch (event.type) {
   139         case SDL_KEYDOWN:
   140         case SDL_KEYUP:
   141             PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
   142             break;
   143         case SDL_TEXTINPUT:
   144             PrintText(event.text.text);
   145             break;
   146         case SDL_MOUSEBUTTONDOWN:
   147             /* Any button press quits the app... */
   148         case SDL_QUIT:
   149             done = 1;
   150             break;
   151         default:
   152             break;
   153         }
   154     }
   155 
   156     SDL_Quit();
   157     return (0);
   158 }