test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 05 Feb 2008 07:19:23 +0000
changeset 2303 d87417504c75
parent 2300 c97ad1abe05b
child 2305 fbe8ff44c519
permissions -rw-r--r--
First pass implementation of new SDL scancode concept, as discussed with
Christian Walther. Currently only implemented on Mac OS X for sanity
checking purposes.
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@1895
    22
print_modifiers(void)
slouken@0
    23
{
slouken@1895
    24
    int mod;
slouken@1895
    25
    printf(" modifiers:");
slouken@1895
    26
    mod = SDL_GetModState();
slouken@1895
    27
    if (!mod) {
slouken@1895
    28
        printf(" (none)");
slouken@1895
    29
        return;
slouken@1895
    30
    }
slouken@1895
    31
    if (mod & KMOD_LSHIFT)
slouken@1895
    32
        printf(" LSHIFT");
slouken@1895
    33
    if (mod & KMOD_RSHIFT)
slouken@1895
    34
        printf(" RSHIFT");
slouken@1895
    35
    if (mod & KMOD_LCTRL)
slouken@1895
    36
        printf(" LCTRL");
slouken@1895
    37
    if (mod & KMOD_RCTRL)
slouken@1895
    38
        printf(" RCTRL");
slouken@1895
    39
    if (mod & KMOD_LALT)
slouken@1895
    40
        printf(" LALT");
slouken@1895
    41
    if (mod & KMOD_RALT)
slouken@1895
    42
        printf(" RALT");
slouken@2303
    43
    if (mod & KMOD_LGUI)
slouken@2303
    44
        printf(" LGUI");
slouken@2303
    45
    if (mod & KMOD_RGUI)
slouken@2303
    46
        printf(" RGUI");
slouken@1895
    47
    if (mod & KMOD_NUM)
slouken@1895
    48
        printf(" NUM");
slouken@1895
    49
    if (mod & KMOD_CAPS)
slouken@1895
    50
        printf(" CAPS");
slouken@1895
    51
    if (mod & KMOD_MODE)
slouken@1895
    52
        printf(" MODE");
slouken@0
    53
}
slouken@0
    54
slouken@1895
    55
static void
slouken@1895
    56
PrintKey(SDL_keysym * sym, int pressed)
slouken@0
    57
{
slouken@1895
    58
    /* Print the keycode, name and state */
slouken@1895
    59
    if (sym->sym) {
slouken@2303
    60
        printf("Key %s:  scancode 0x%04X = %s, keycode 0x%08X = %s ",
bob@2295
    61
               pressed ? "pressed " : "released",
slouken@2303
    62
               sym->scancode,
slouken@2303
    63
               SDL_GetScancodeName(sym->scancode),
slouken@2303
    64
               sym->sym, SDL_GetKeyName(sym->sym));
slouken@1895
    65
    } else {
slouken@2303
    66
        printf("Unknown Key (scancode = 0x%04X) %s ",
bob@2295
    67
               sym->scancode, pressed ? "pressed" : "released");
slouken@1895
    68
    }
slouken@0
    69
slouken@1895
    70
    /* Print the translated character, if one exists */
slouken@1895
    71
    if (sym->unicode) {
slouken@1895
    72
        /* Is it a control-character? */
slouken@1895
    73
        if (sym->unicode < ' ') {
slouken@1895
    74
            printf(" (^%c)", sym->unicode + '@');
slouken@1895
    75
        } else {
slouken@0
    76
#ifdef UNICODE
slouken@1895
    77
            printf(" (%c)", sym->unicode);
slouken@0
    78
#else
slouken@1895
    79
            /* This is a Latin-1 program, so only show 8-bits */
slouken@1895
    80
            if (!(sym->unicode & 0xFF00))
slouken@1895
    81
                printf(" (%c)", sym->unicode);
slouken@1895
    82
            else
slouken@1895
    83
                printf(" (0x%X)", sym->unicode);
slouken@0
    84
#endif
slouken@1895
    85
        }
slouken@1895
    86
    }
slouken@1895
    87
    print_modifiers();
slouken@1895
    88
    printf("\n");
slouken@0
    89
}
slouken@0
    90
bob@2300
    91
static void
bob@2300
    92
PrintText(char *text)
bob@2300
    93
{
bob@2300
    94
}
bob@2300
    95
slouken@1895
    96
int
slouken@1895
    97
main(int argc, char *argv[])
slouken@0
    98
{
slouken@1895
    99
    SDL_Event event;
slouken@1895
   100
    int done;
slouken@1895
   101
    Uint32 videoflags;
slouken@0
   102
slouken@1895
   103
    /* Initialize SDL */
slouken@1895
   104
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@1895
   105
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   106
        return (1);
slouken@1895
   107
    }
slouken@0
   108
slouken@1895
   109
    videoflags = SDL_SWSURFACE;
slouken@1895
   110
    while (argc > 1) {
slouken@1895
   111
        --argc;
slouken@1895
   112
        if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
slouken@1895
   113
            videoflags |= SDL_FULLSCREEN;
slouken@1895
   114
        } else {
slouken@1895
   115
            fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
slouken@1895
   116
            quit(1);
slouken@1895
   117
        }
slouken@1895
   118
    }
slouken@473
   119
slouken@1895
   120
    /* Set 640x480 video mode */
slouken@1895
   121
    if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
slouken@1895
   122
        fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
slouken@1895
   123
                SDL_GetError());
slouken@1895
   124
        quit(2);
slouken@1895
   125
    }
slouken@0
   126
slouken@1895
   127
    /* Enable UNICODE translation for keyboard input */
slouken@1895
   128
    SDL_EnableUNICODE(1);
slouken@0
   129
slouken@1895
   130
    /* Enable auto repeat for keyboard input */
slouken@1895
   131
    SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
slouken@1895
   132
                        SDL_DEFAULT_REPEAT_INTERVAL);
slouken@0
   133
slouken@1895
   134
    /* Watch keystrokes */
slouken@1895
   135
    done = 0;
slouken@1895
   136
    while (!done) {
slouken@1895
   137
        /* Check for events */
slouken@1895
   138
        SDL_WaitEvent(&event);
slouken@1895
   139
        switch (event.type) {
slouken@1895
   140
        case SDL_KEYDOWN:
slouken@1895
   141
            PrintKey(&event.key.keysym, 1);
slouken@1895
   142
            break;
slouken@1895
   143
        case SDL_KEYUP:
slouken@1895
   144
            PrintKey(&event.key.keysym, 0);
slouken@1895
   145
            break;
bob@2300
   146
        case SDL_TEXTINPUT:
bob@2300
   147
            PrintText(event.text.text);
bob@2300
   148
            break;
slouken@1895
   149
        case SDL_MOUSEBUTTONDOWN:
slouken@1895
   150
            /* Any button press quits the app... */
slouken@1895
   151
        case SDL_QUIT:
slouken@1895
   152
            done = 1;
slouken@1895
   153
            break;
slouken@1895
   154
        default:
slouken@1895
   155
            break;
slouken@1895
   156
        }
slouken@1895
   157
    }
icculus@1151
   158
slouken@1895
   159
    SDL_Quit();
slouken@1895
   160
    return (0);
slouken@0
   161
}