test/checkkeys.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 01 May 2015 01:12:48 -0400
changeset 9580 d37ef6990bf9
parent 9278 8900afb78a19
child 9581 4da24acfbd90
permissions -rw-r--r--
checkkeys: Readded the KEYUP event test.
     1 /*
     2   Copyright (C) 1997-2014 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 #ifdef __EMSCRIPTEN__
    23 #include <emscripten/emscripten.h>
    24 #endif
    25 
    26 #include "SDL.h"
    27 
    28 int done;
    29 
    30 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    31 static void
    32 quit(int rc)
    33 {
    34     SDL_Quit();
    35     exit(rc);
    36 }
    37 
    38 static void
    39 print_string(char **text, size_t *maxlen, const char *fmt, ...)
    40 {
    41     int len;
    42     va_list ap;
    43 
    44     va_start(ap, fmt);
    45     len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
    46     if (len > 0) {
    47         *text += len;
    48         if ( ((size_t) len) < *maxlen ) {
    49             *maxlen -= (size_t) len;
    50         } else {
    51             *maxlen = 0;
    52         }
    53     }
    54     va_end(ap);
    55 }
    56 
    57 static void
    58 print_modifiers(char **text, size_t *maxlen)
    59 {
    60     int mod;
    61     print_string(text, maxlen, " modifiers:");
    62     mod = SDL_GetModState();
    63     if (!mod) {
    64         print_string(text, maxlen, " (none)");
    65         return;
    66     }
    67     if (mod & KMOD_LSHIFT)
    68         print_string(text, maxlen, " LSHIFT");
    69     if (mod & KMOD_RSHIFT)
    70         print_string(text, maxlen, " RSHIFT");
    71     if (mod & KMOD_LCTRL)
    72         print_string(text, maxlen, " LCTRL");
    73     if (mod & KMOD_RCTRL)
    74         print_string(text, maxlen, " RCTRL");
    75     if (mod & KMOD_LALT)
    76         print_string(text, maxlen, " LALT");
    77     if (mod & KMOD_RALT)
    78         print_string(text, maxlen, " RALT");
    79     if (mod & KMOD_LGUI)
    80         print_string(text, maxlen, " LGUI");
    81     if (mod & KMOD_RGUI)
    82         print_string(text, maxlen, " RGUI");
    83     if (mod & KMOD_NUM)
    84         print_string(text, maxlen, " NUM");
    85     if (mod & KMOD_CAPS)
    86         print_string(text, maxlen, " CAPS");
    87     if (mod & KMOD_MODE)
    88         print_string(text, maxlen, " MODE");
    89 }
    90 
    91 static void
    92 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    93 {
    94     char message[512];
    95     char *spot;
    96     size_t left;
    97 
    98     spot = message;
    99     left = sizeof(message);
   100 
   101     /* Print the keycode, name and state */
   102     if (sym->sym) {
   103         print_string(&spot, &left,
   104                 "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
   105                 pressed ? "pressed " : "released",
   106                 sym->scancode,
   107                 SDL_GetScancodeName(sym->scancode),
   108                 sym->sym, SDL_GetKeyName(sym->sym));
   109     } else {
   110         print_string(&spot, &left,
   111                 "Unknown Key (scancode %d = %s) %s ",
   112                 sym->scancode,
   113                 SDL_GetScancodeName(sym->scancode),
   114                 pressed ? "pressed " : "released");
   115     }
   116     print_modifiers(&spot, &left);
   117     if (repeat) {
   118         print_string(&spot, &left, " (repeat)");
   119     }
   120     SDL_Log("%s\n", message);
   121 }
   122 
   123 static void
   124 PrintText(char *text)
   125 {
   126     char *spot, expanded[1024];
   127 
   128     expanded[0] = '\0';
   129     for ( spot = text; *spot; ++spot )
   130     {
   131         size_t length = SDL_strlen(expanded);
   132         SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
   133     }
   134     SDL_Log("Text (%s): \"%s%s\"\n", expanded, *text == '"' ? "\\" : "", text);
   135 }
   136 
   137 void
   138 loop()
   139 {
   140     SDL_Event event;
   141     /* Check for events */
   142     /*SDL_WaitEvent(&event); emscripten does not like waiting*/
   143 
   144     while (SDL_PollEvent(&event)) {
   145         switch (event.type) {
   146         case SDL_KEYDOWN:
   147         case SDL_KEYUP:
   148 		    PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
   149             break;
   150         case SDL_TEXTINPUT:
   151             PrintText(event.text.text);
   152             break;
   153         case SDL_MOUSEBUTTONDOWN:
   154             /* Any button press quits the app... */
   155         case SDL_QUIT:
   156             done = 1;
   157             break;
   158         default:
   159             break;
   160         }
   161     }
   162 }
   163 
   164 int
   165 main(int argc, char *argv[])
   166 {
   167     SDL_Window *window;
   168 	
   169 	/* Enable standard application logging */
   170 	SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   171 
   172     /* Initialize SDL */
   173     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   174 		SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
   175         return (1);
   176     }
   177 
   178     /* Set 640x480 video mode */
   179     window = SDL_CreateWindow("CheckKeys Test",
   180                               SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   181                               640, 480, 0);
   182     if (!window) {
   183         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
   184                 SDL_GetError());
   185         quit(2);
   186     }
   187 
   188 #if __IPHONEOS__
   189     /* Creating the context creates the view, which we need to show keyboard */
   190     SDL_GL_CreateContext(window);
   191 #endif
   192 
   193     SDL_StartTextInput();
   194 
   195     /* Watch keystrokes */
   196     done = 0;
   197 
   198 #ifdef __EMSCRIPTEN__
   199     emscripten_set_main_loop(loop, 0, 1);
   200 #else
   201     while (!done) {
   202         loop();
   203     }
   204 #endif
   205 
   206     SDL_Quit();
   207     return (0);
   208 }
   209 
   210 /* vi: set ts=4 sw=4 expandtab: */