test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Fri, 08 Apr 2011 13:03:26 -0700
changeset 5535 96594ac5fd1a
parent 5461 94f742ce580a
child 5879 cdb371a9b4a1
permissions -rw-r--r--
SDL 1.3 is now under the zlib license.
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 /* Simple program:  Loop, watching keystrokes
    14    Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to 
    15    pump the event loop and catch keystrokes.
    16 */
    17 
    18 #include <stdio.h>
    19 #include <stdlib.h>
    20 #include <string.h>
    21 
    22 #include "SDL.h"
    23 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    26 quit(int rc)
    27 {
    28     SDL_Quit();
    29     exit(rc);
    30 }
    31 
    32 static void
    33 print_string(char **text, size_t *maxlen, const char *fmt, ...)
    34 {
    35     int len;
    36     va_list ap;
    37 
    38     va_start(ap, fmt);
    39     len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
    40     if (len > 0) {
    41         *text += len;
    42         if (len < *maxlen) {
    43             *maxlen -= len;
    44         } else {
    45             *maxlen = 0;
    46         }
    47     }
    48     va_end(ap);
    49 }
    50 
    51 static void
    52 print_modifiers(char **text, size_t *maxlen)
    53 {
    54     int mod;
    55     print_string(text, maxlen, " modifiers:");
    56     mod = SDL_GetModState();
    57     if (!mod) {
    58         print_string(text, maxlen, " (none)");
    59         return;
    60     }
    61     if (mod & KMOD_LSHIFT)
    62         print_string(text, maxlen, " LSHIFT");
    63     if (mod & KMOD_RSHIFT)
    64         print_string(text, maxlen, " RSHIFT");
    65     if (mod & KMOD_LCTRL)
    66         print_string(text, maxlen, " LCTRL");
    67     if (mod & KMOD_RCTRL)
    68         print_string(text, maxlen, " RCTRL");
    69     if (mod & KMOD_LALT)
    70         print_string(text, maxlen, " LALT");
    71     if (mod & KMOD_RALT)
    72         print_string(text, maxlen, " RALT");
    73     if (mod & KMOD_LGUI)
    74         print_string(text, maxlen, " LGUI");
    75     if (mod & KMOD_RGUI)
    76         print_string(text, maxlen, " RGUI");
    77     if (mod & KMOD_NUM)
    78         print_string(text, maxlen, " NUM");
    79     if (mod & KMOD_CAPS)
    80         print_string(text, maxlen, " CAPS");
    81     if (mod & KMOD_MODE)
    82         print_string(text, maxlen, " MODE");
    83 }
    84 
    85 static void
    86 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    87 {
    88     char message[512];
    89     char *spot;
    90     size_t left;
    91 
    92     spot = message;
    93     left = sizeof(message);
    94 
    95     /* Print the keycode, name and state */
    96     if (sym->sym) {
    97         print_string(&spot, &left,
    98                 "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    99                 pressed ? "pressed " : "released",
   100                 sym->scancode,
   101                 SDL_GetScancodeName(sym->scancode),
   102                 sym->sym, SDL_GetKeyName(sym->sym));
   103     } else {
   104         print_string(&spot, &left,
   105                 "Unknown Key (scancode %d = %s) %s ",
   106                 sym->scancode,
   107                 SDL_GetScancodeName(sym->scancode),
   108                 pressed ? "pressed" : "released");
   109     }
   110 
   111     /* Print the translated character, if one exists */
   112     if (sym->unicode) {
   113         /* Is it a control-character? */
   114         if (sym->unicode < ' ') {
   115             print_string(&spot, &left, " (^%c)", sym->unicode + '@');
   116         } else {
   117 #ifdef UNICODE
   118             print_string(&spot, &left, " (%c)", sym->unicode);
   119 #else
   120             /* This is a Latin-1 program, so only show 8-bits */
   121             if (!(sym->unicode & 0xFF00))
   122                 print_string(&spot, &left, " (%c)", sym->unicode);
   123             else
   124                 print_string(&spot, &left, " (0x%X)", sym->unicode);
   125 #endif
   126         }
   127     }
   128     print_modifiers(&spot, &left);
   129     if (repeat) {
   130         print_string(&spot, &left, " (repeat)");
   131     }
   132     SDL_Log("%s", message);
   133 }
   134 
   135 static void
   136 PrintText(char *text)
   137 {
   138     SDL_Log("Text: %s", text);
   139 }
   140 
   141 #if __IPHONEOS__
   142 extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
   143 #endif
   144 
   145 int
   146 main(int argc, char *argv[])
   147 {
   148     SDL_Window *window;
   149     SDL_Event event;
   150     int done;
   151 
   152     /* Initialize SDL */
   153     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   154         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   155         return (1);
   156     }
   157 
   158     /* Set 640x480 video mode */
   159     window = SDL_CreateWindow("CheckKeys Test",
   160                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   161                               640, 480, 0);
   162     if (!window) {
   163         fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   164                 SDL_GetError());
   165         quit(2);
   166     }
   167 
   168 #if __IPHONEOS__
   169     /* Creating the context creates the view, which we need to show keyboard */
   170     SDL_GL_CreateContext(window);
   171     SDL_iPhoneKeyboardShow(window);
   172 #endif
   173     
   174 
   175     /* Watch keystrokes */
   176     done = 0;
   177     while (!done) {
   178         /* Check for events */
   179         SDL_WaitEvent(&event);
   180         switch (event.type) {
   181         case SDL_KEYDOWN:
   182         case SDL_KEYUP:
   183             PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
   184             break;
   185         case SDL_TEXTINPUT:
   186             PrintText(event.text.text);
   187             break;
   188         case SDL_MOUSEBUTTONDOWN:
   189             /* Any button press quits the app... */
   190         case SDL_QUIT:
   191             done = 1;
   192             break;
   193         default:
   194             break;
   195         }
   196     }
   197 
   198     SDL_Quit();
   199     return (0);
   200 }
   201 
   202 /* vi: set ts=4 sw=4 expandtab: */