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