test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 09 Mar 2011 17:38:12 -0800
changeset 5461 94f742ce580a
parent 5218 572a73d71b5f
child 5535 96594ac5fd1a
permissions -rw-r--r--
Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
     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_string(char **text, size_t *maxlen, const char *fmt, ...)
    23 {
    24     int len;
    25     va_list ap;
    26 
    27     va_start(ap, fmt);
    28     len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
    29     if (len > 0) {
    30         *text += len;
    31         if (len < *maxlen) {
    32             *maxlen -= len;
    33         } else {
    34             *maxlen = 0;
    35         }
    36     }
    37     va_end(ap);
    38 }
    39 
    40 static void
    41 print_modifiers(char **text, size_t *maxlen)
    42 {
    43     int mod;
    44     print_string(text, maxlen, " modifiers:");
    45     mod = SDL_GetModState();
    46     if (!mod) {
    47         print_string(text, maxlen, " (none)");
    48         return;
    49     }
    50     if (mod & KMOD_LSHIFT)
    51         print_string(text, maxlen, " LSHIFT");
    52     if (mod & KMOD_RSHIFT)
    53         print_string(text, maxlen, " RSHIFT");
    54     if (mod & KMOD_LCTRL)
    55         print_string(text, maxlen, " LCTRL");
    56     if (mod & KMOD_RCTRL)
    57         print_string(text, maxlen, " RCTRL");
    58     if (mod & KMOD_LALT)
    59         print_string(text, maxlen, " LALT");
    60     if (mod & KMOD_RALT)
    61         print_string(text, maxlen, " RALT");
    62     if (mod & KMOD_LGUI)
    63         print_string(text, maxlen, " LGUI");
    64     if (mod & KMOD_RGUI)
    65         print_string(text, maxlen, " RGUI");
    66     if (mod & KMOD_NUM)
    67         print_string(text, maxlen, " NUM");
    68     if (mod & KMOD_CAPS)
    69         print_string(text, maxlen, " CAPS");
    70     if (mod & KMOD_MODE)
    71         print_string(text, maxlen, " MODE");
    72 }
    73 
    74 static void
    75 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    76 {
    77     char message[512];
    78     char *spot;
    79     size_t left;
    80 
    81     spot = message;
    82     left = sizeof(message);
    83 
    84     /* Print the keycode, name and state */
    85     if (sym->sym) {
    86         print_string(&spot, &left,
    87                 "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    88                 pressed ? "pressed " : "released",
    89                 sym->scancode,
    90                 SDL_GetScancodeName(sym->scancode),
    91                 sym->sym, SDL_GetKeyName(sym->sym));
    92     } else {
    93         print_string(&spot, &left,
    94                 "Unknown Key (scancode %d = %s) %s ",
    95                 sym->scancode,
    96                 SDL_GetScancodeName(sym->scancode),
    97                 pressed ? "pressed" : "released");
    98     }
    99 
   100     /* Print the translated character, if one exists */
   101     if (sym->unicode) {
   102         /* Is it a control-character? */
   103         if (sym->unicode < ' ') {
   104             print_string(&spot, &left, " (^%c)", sym->unicode + '@');
   105         } else {
   106 #ifdef UNICODE
   107             print_string(&spot, &left, " (%c)", sym->unicode);
   108 #else
   109             /* This is a Latin-1 program, so only show 8-bits */
   110             if (!(sym->unicode & 0xFF00))
   111                 print_string(&spot, &left, " (%c)", sym->unicode);
   112             else
   113                 print_string(&spot, &left, " (0x%X)", sym->unicode);
   114 #endif
   115         }
   116     }
   117     print_modifiers(&spot, &left);
   118     if (repeat) {
   119         print_string(&spot, &left, " (repeat)");
   120     }
   121     SDL_Log("%s", message);
   122 }
   123 
   124 static void
   125 PrintText(char *text)
   126 {
   127     SDL_Log("Text: %s", text);
   128 }
   129 
   130 #if __IPHONEOS__
   131 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
   132 #endif
   133 
   134 int
   135 main(int argc, char *argv[])
   136 {
   137     SDL_Window *window;
   138     SDL_Event event;
   139     int done;
   140 
   141     /* Initialize SDL */
   142     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   143         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   144         return (1);
   145     }
   146 
   147     /* Set 640x480 video mode */
   148     window = SDL_CreateWindow("CheckKeys Test",
   149                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   150                               640, 480, 0);
   151     if (!window) {
   152         fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   153                 SDL_GetError());
   154         quit(2);
   155     }
   156 
   157 #if __IPHONEOS__
   158     /* Creating the context creates the view, which we need to show keyboard */
   159     SDL_GL_CreateContext(window);
   160     SDL_iPhoneKeyboardShow(window);
   161 #endif
   162     
   163 
   164     /* Watch keystrokes */
   165     done = 0;
   166     while (!done) {
   167         /* Check for events */
   168         SDL_WaitEvent(&event);
   169         switch (event.type) {
   170         case SDL_KEYDOWN:
   171         case SDL_KEYUP:
   172             PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
   173             break;
   174         case SDL_TEXTINPUT:
   175             PrintText(event.text.text);
   176             break;
   177         case SDL_MOUSEBUTTONDOWN:
   178             /* Any button press quits the app... */
   179         case SDL_QUIT:
   180             done = 1;
   181             break;
   182         default:
   183             break;
   184         }
   185     }
   186 
   187     SDL_Quit();
   188     return (0);
   189 }
   190 
   191 /* vi: set ts=4 sw=4 expandtab: */