test/checkkeys.c
author David Ludwig <dludwig@pobox.com>
Tue, 12 Feb 2013 12:57:06 -0500
changeset 8424 3cf9501008f1
parent 6654 2ecfb25be1e2
child 6922 91d157d9f283
permissions -rw-r--r--
WinRT: fixed bug: SDL_CreateWindow wouldn't work after an initial window was created + destroyed
     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 ( ((size_t) len) < *maxlen ) {
    43             *maxlen -= (size_t) 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 int
   142 main(int argc, char *argv[])
   143 {
   144     SDL_Window *window;
   145     SDL_Event event;
   146     int done;
   147 
   148     /* Initialize SDL */
   149     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   150         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   151         return (1);
   152     }
   153 
   154     /* Set 640x480 video mode */
   155     window = SDL_CreateWindow("CheckKeys Test",
   156                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   157                               640, 480, 0);
   158     if (!window) {
   159         fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   160                 SDL_GetError());
   161         quit(2);
   162     }
   163 
   164 #if __IPHONEOS__
   165     /* Creating the context creates the view, which we need to show keyboard */
   166     SDL_GL_CreateContext(window);
   167 #endif
   168 
   169     SDL_StartTextInput();
   170 
   171     /* Watch keystrokes */
   172     done = 0;
   173     while (!done) {
   174         /* Check for events */
   175         SDL_WaitEvent(&event);
   176         switch (event.type) {
   177         case SDL_KEYDOWN:
   178         case SDL_KEYUP:
   179             PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
   180             break;
   181         case SDL_TEXTINPUT:
   182             PrintText(event.text.text);
   183             break;
   184         case SDL_MOUSEBUTTONDOWN:
   185             /* Any button press quits the app... */
   186         case SDL_QUIT:
   187             done = 1;
   188             break;
   189         default:
   190             break;
   191         }
   192     }
   193 
   194     SDL_Quit();
   195     return (0);
   196 }
   197 
   198 /* vi: set ts=4 sw=4 expandtab: */