test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Feb 2008 07:19:23 +0000
changeset 2303 d87417504c75
parent 2300 c97ad1abe05b
child 2305 fbe8ff44c519
permissions -rw-r--r--
First pass implementation of new SDL scancode concept, as discussed with
Christian Walther. Currently only implemented on Mac OS X for sanity
checking purposes.
     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 }
    95 
    96 int
    97 main(int argc, char *argv[])
    98 {
    99     SDL_Event event;
   100     int done;
   101     Uint32 videoflags;
   102 
   103     /* Initialize SDL */
   104     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   105         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   106         return (1);
   107     }
   108 
   109     videoflags = SDL_SWSURFACE;
   110     while (argc > 1) {
   111         --argc;
   112         if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   113             videoflags |= SDL_FULLSCREEN;
   114         } else {
   115             fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   116             quit(1);
   117         }
   118     }
   119 
   120     /* Set 640x480 video mode */
   121     if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   122         fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   123                 SDL_GetError());
   124         quit(2);
   125     }
   126 
   127     /* Enable UNICODE translation for keyboard input */
   128     SDL_EnableUNICODE(1);
   129 
   130     /* Enable auto repeat for keyboard input */
   131     SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
   132                         SDL_DEFAULT_REPEAT_INTERVAL);
   133 
   134     /* Watch keystrokes */
   135     done = 0;
   136     while (!done) {
   137         /* Check for events */
   138         SDL_WaitEvent(&event);
   139         switch (event.type) {
   140         case SDL_KEYDOWN:
   141             PrintKey(&event.key.keysym, 1);
   142             break;
   143         case SDL_KEYUP:
   144             PrintKey(&event.key.keysym, 0);
   145             break;
   146         case SDL_TEXTINPUT:
   147             PrintText(event.text.text);
   148             break;
   149         case SDL_MOUSEBUTTONDOWN:
   150             /* Any button press quits the app... */
   151         case SDL_QUIT:
   152             done = 1;
   153             break;
   154         default:
   155             break;
   156         }
   157     }
   158 
   159     SDL_Quit();
   160     return (0);
   161 }