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