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