test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 18 Jun 2013 00:39:47 -0700
changeset 7312 b36811d7db33
parent 7191 75360622e65f
child 7317 37b77749523b
permissions -rw-r--r--
Fixed bug 1916 - SDL_Keysym contains a deprecated field for unicode which may be removed.

Philipp Wiesemann

SDL_Keysym contains a deprecated field for unicode which may be removed for SDL 2.0 release.

As far as I can tell the field is not set on all "major" platforms and therefore will not be useful for most users. Its existence in a public header therefore becomes (in my opinion) only confusing.
     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     print_modifiers(&spot, &left);
   111     if (repeat) {
   112         print_string(&spot, &left, " (repeat)");
   113     }
   114     SDL_Log("%s\n", message);
   115 }
   116 
   117 static void
   118 PrintText(char *text)
   119 {
   120     SDL_Log("Text: %s\n", text);
   121 }
   122 
   123 int
   124 main(int argc, char *argv[])
   125 {
   126     SDL_Window *window;
   127     SDL_Event event;
   128     int done;
   129 
   130     /* Initialize SDL */
   131     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   132         fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
   133         return (1);
   134     }
   135 
   136     /* Set 640x480 video mode */
   137     window = SDL_CreateWindow("CheckKeys Test",
   138                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   139                               640, 480, 0);
   140     if (!window) {
   141         fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   142                 SDL_GetError());
   143         quit(2);
   144     }
   145 
   146 #if __IPHONEOS__
   147     /* Creating the context creates the view, which we need to show keyboard */
   148     SDL_GL_CreateContext(window);
   149 #endif
   150 
   151     SDL_StartTextInput();
   152 
   153     /* Watch keystrokes */
   154     done = 0;
   155     while (!done) {
   156         /* Check for events */
   157         SDL_WaitEvent(&event);
   158         switch (event.type) {
   159         case SDL_KEYDOWN:
   160         case SDL_KEYUP:
   161             PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
   162             break;
   163         case SDL_TEXTINPUT:
   164             PrintText(event.text.text);
   165             break;
   166         case SDL_MOUSEBUTTONDOWN:
   167             /* Any button press quits the app... */
   168         case SDL_QUIT:
   169             done = 1;
   170             break;
   171         default:
   172             break;
   173         }
   174     }
   175 
   176     SDL_Quit();
   177     return (0);
   178 }
   179 
   180 /* vi: set ts=4 sw=4 expandtab: */