test/checkkeys.c
author Bob Pendleton <bob@pendleton.com>
Tue, 08 Jan 2008 00:10:46 +0000
changeset 2295 dbc6d1893869
parent 1895 c121d94672cb
child 2300 c97ad1abe05b
permissions -rw-r--r--
Checking in Christian Walther's patch for x11 keyboard input. Minor code tweaks by Bob.
     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:  physical 0x%08X = %s, layout 0x%08X = %s ",
    61                pressed ? "pressed " : "released",
    62                sym->sym,
    63                SDL_GetKeyName(sym->sym),
    64                SDL_GetLayoutKey(sym->sym),
    65                SDL_GetKeyName(SDL_GetLayoutKey(sym->sym)));
    66     } else {
    67         printf("Unknown Key (scancode = 0x%08X) %s ",
    68                sym->scancode, pressed ? "pressed" : "released");
    69     }
    70 
    71     /* Print the translated character, if one exists */
    72     if (sym->unicode) {
    73         /* Is it a control-character? */
    74         if (sym->unicode < ' ') {
    75             printf(" (^%c)", sym->unicode + '@');
    76         } else {
    77 #ifdef UNICODE
    78             printf(" (%c)", sym->unicode);
    79 #else
    80             /* This is a Latin-1 program, so only show 8-bits */
    81             if (!(sym->unicode & 0xFF00))
    82                 printf(" (%c)", sym->unicode);
    83             else
    84                 printf(" (0x%X)", sym->unicode);
    85 #endif
    86         }
    87     }
    88     print_modifiers();
    89     printf("\n");
    90 }
    91 
    92 int
    93 main(int argc, char *argv[])
    94 {
    95     SDL_Event event;
    96     int done;
    97     Uint32 videoflags;
    98 
    99     /* Initialize SDL */
   100     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   101         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   102         return (1);
   103     }
   104 
   105     videoflags = SDL_SWSURFACE;
   106     while (argc > 1) {
   107         --argc;
   108         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   109             videoflags |= SDL_FULLSCREEN;
   110         } else {
   111             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   112             quit(1);
   113         }
   114     }
   115 
   116     /* Set 640x480 video mode */
   117     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   118         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   119                 SDL_GetError());
   120         quit(2);
   121     }
   122 
   123     /* Enable UNICODE translation for keyboard input */
   124     SDL_EnableUNICODE(1);
   125 
   126     /* Enable auto repeat for keyboard input */
   127     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
   128                         SDL_DEFAULT_REPEAT_INTERVAL);
   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_MOUSEBUTTONDOWN:
   143             /* Any button press quits the app... */
   144         case SDL_QUIT:
   145             done = 1;
   146             break;
   147         default:
   148             break;
   149         }
   150     }
   151 
   152     SDL_Quit();
   153     return (0);
   154 }