test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 28 May 2006 13:04:16 +0000
branchSDL-1.3
changeset 1662 782fd950bd46
parent 1253 7c7ddaf195bf
child 1668 4da1ee79c9af
permissions -rw-r--r--
Revamp of the video system in progress - adding support for multiple displays, multiple windows, and a full video mode selection API.

WARNING: None of the video drivers have been updated for the new API yet! The API is still under design and very fluid.

The code is now run through a consistent indent format:
indent -i4 -nut -nsc -br -ce

The headers are being converted to automatically generate doxygen documentation.
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@1662
    14
static void
slouken@1662
    15
quit (int rc)
icculus@1151
    16
{
slouken@1662
    17
    SDL_Quit ();
slouken@1662
    18
    exit (rc);
icculus@1151
    19
}
icculus@1151
    20
slouken@1662
    21
static void
slouken@1662
    22
print_modifiers (void)
slouken@0
    23
{
slouken@1662
    24
    int mod;
slouken@1662
    25
    printf (" modifiers:");
slouken@1662
    26
    mod = SDL_GetModState ();
slouken@1662
    27
    if (!mod) {
slouken@1662
    28
        printf (" (none)");
slouken@1662
    29
        return;
slouken@1662
    30
    }
slouken@1662
    31
    if (mod & KMOD_LSHIFT)
slouken@1662
    32
        printf (" LSHIFT");
slouken@1662
    33
    if (mod & KMOD_RSHIFT)
slouken@1662
    34
        printf (" RSHIFT");
slouken@1662
    35
    if (mod & KMOD_LCTRL)
slouken@1662
    36
        printf (" LCTRL");
slouken@1662
    37
    if (mod & KMOD_RCTRL)
slouken@1662
    38
        printf (" RCTRL");
slouken@1662
    39
    if (mod & KMOD_LALT)
slouken@1662
    40
        printf (" LALT");
slouken@1662
    41
    if (mod & KMOD_RALT)
slouken@1662
    42
        printf (" RALT");
slouken@1662
    43
    if (mod & KMOD_LMETA)
slouken@1662
    44
        printf (" LMETA");
slouken@1662
    45
    if (mod & KMOD_RMETA)
slouken@1662
    46
        printf (" RMETA");
slouken@1662
    47
    if (mod & KMOD_NUM)
slouken@1662
    48
        printf (" NUM");
slouken@1662
    49
    if (mod & KMOD_CAPS)
slouken@1662
    50
        printf (" CAPS");
slouken@1662
    51
    if (mod & KMOD_MODE)
slouken@1662
    52
        printf (" MODE");
slouken@0
    53
}
slouken@0
    54
slouken@1662
    55
static void
slouken@1662
    56
PrintKey (SDL_keysym * sym, int pressed)
slouken@0
    57
{
slouken@1662
    58
    /* Print the keycode, name and state */
slouken@1662
    59
    if (sym->sym) {
slouken@1662
    60
        printf ("Key %s:  %d-%s ", pressed ? "pressed" : "released",
slouken@1662
    61
                sym->sym, SDL_GetKeyName (sym->sym));
slouken@1662
    62
    } else {
slouken@1662
    63
        printf ("Unknown Key (scancode = %d) %s ", sym->scancode,
slouken@1662
    64
                pressed ? "pressed" : "released");
slouken@1662
    65
    }
slouken@0
    66
slouken@1662
    67
    /* Print the translated character, if one exists */
slouken@1662
    68
    if (sym->unicode) {
slouken@1662
    69
        /* Is it a control-character? */
slouken@1662
    70
        if (sym->unicode < ' ') {
slouken@1662
    71
            printf (" (^%c)", sym->unicode + '@');
slouken@1662
    72
        } else {
slouken@0
    73
#ifdef UNICODE
slouken@1662
    74
            printf (" (%c)", sym->unicode);
slouken@0
    75
#else
slouken@1662
    76
            /* This is a Latin-1 program, so only show 8-bits */
slouken@1662
    77
            if (!(sym->unicode & 0xFF00))
slouken@1662
    78
                printf (" (%c)", sym->unicode);
slouken@1662
    79
            else
slouken@1662
    80
                printf (" (0x%X)", sym->unicode);
slouken@0
    81
#endif
slouken@1662
    82
        }
slouken@1662
    83
    }
slouken@1662
    84
    print_modifiers ();
slouken@1662
    85
    printf ("\n");
slouken@0
    86
}
slouken@0
    87
slouken@1662
    88
int
slouken@1662
    89
main (int argc, char *argv[])
slouken@0
    90
{
slouken@1662
    91
    SDL_Event event;
slouken@1662
    92
    int done;
slouken@1662
    93
    Uint32 videoflags;
slouken@0
    94
slouken@1662
    95
    /* Initialize SDL */
slouken@1662
    96
    if (SDL_Init (SDL_INIT_VIDEO) < 0) {
slouken@1662
    97
        fprintf (stderr, "Couldn't initialize SDL: %s\n", SDL_GetError ());
slouken@1662
    98
        return (1);
slouken@1662
    99
    }
slouken@0
   100
slouken@1662
   101
    videoflags = SDL_SWSURFACE;
slouken@1662
   102
    while (argc > 1) {
slouken@1662
   103
        --argc;
slouken@1662
   104
        if (argv[argc] && !strcmp (argv[argc], "-fullscreen")) {
slouken@1662
   105
            videoflags |= SDL_FULLSCREEN;
slouken@1662
   106
        } else {
slouken@1662
   107
            fprintf (stderr, "Usage: %s [-fullscreen]\n", argv[0]);
slouken@1662
   108
            quit (1);
slouken@1662
   109
        }
slouken@1662
   110
    }
slouken@473
   111
slouken@1662
   112
    /* Set 640x480 video mode */
slouken@1662
   113
    if (SDL_SetVideoMode (640, 480, 0, videoflags) == NULL) {
slouken@1662
   114
        fprintf (stderr, "Couldn't set 640x480 video mode: %s\n",
slouken@1662
   115
                 SDL_GetError ());
slouken@1662
   116
        quit (2);
slouken@1662
   117
    }
slouken@0
   118
slouken@1662
   119
    /* Enable UNICODE translation for keyboard input */
slouken@1662
   120
    SDL_EnableUNICODE (1);
slouken@0
   121
slouken@1662
   122
    /* Enable auto repeat for keyboard input */
slouken@1662
   123
    SDL_EnableKeyRepeat (SDL_DEFAULT_REPEAT_DELAY,
slouken@1662
   124
                         SDL_DEFAULT_REPEAT_INTERVAL);
slouken@0
   125
slouken@1662
   126
    /* Watch keystrokes */
slouken@1662
   127
    done = 0;
slouken@1662
   128
    while (!done) {
slouken@1662
   129
        /* Check for events */
slouken@1662
   130
        SDL_WaitEvent (&event);
slouken@1662
   131
        switch (event.type) {
slouken@1662
   132
        case SDL_KEYDOWN:
slouken@1662
   133
            PrintKey (&event.key.keysym, 1);
slouken@1662
   134
            break;
slouken@1662
   135
        case SDL_KEYUP:
slouken@1662
   136
            PrintKey (&event.key.keysym, 0);
slouken@1662
   137
            break;
slouken@1662
   138
        case SDL_MOUSEBUTTONDOWN:
slouken@1662
   139
            /* Any button press quits the app... */
slouken@1662
   140
        case SDL_QUIT:
slouken@1662
   141
            done = 1;
slouken@1662
   142
            break;
slouken@1662
   143
        default:
slouken@1662
   144
            break;
slouken@1662
   145
        }
slouken@1662
   146
    }
icculus@1151
   147
slouken@1662
   148
    SDL_Quit ();
slouken@1662
   149
    return (0);
slouken@0
   150
}