test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 04 Nov 2012 21:53:28 -0800
changeset 6654 2ecfb25be1e2
parent 6392 fa7eb111f994
child 6922 91d157d9f283
permissions -rw-r--r--
Synchronized the on-screen keyboard state with whether we are accepting text input. The functions to show/hide/toggle the on-screen keyboard have been folded into the text input state. Calling SDL_StartTextInput() will automatically show the on-screen keyboard if it's available. Calling SDL_StopTextInput() will automatically hide the on-screen keyboard if it's available. There is a new API function SDL_IsTextInputActive() which will return whether text input is currently active. Text input is disabled by default, you must call SDL_StartTextInput() when you are ready to accept text input. SDL_HasScreenKeyboardSupport() no longer needs to be passed a window. The iPhone-specific on-screen keyboard functions have been removed.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5535
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     1
/*
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     3
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     4
  This software is provided 'as-is', without any express or implied
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     6
  arising from the use of this software.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     7
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    10
  freely.
96594ac5fd1a SDL 1.3 is now under the zlib license.
Sam Lantinga <slouken@libsdl.org>
parents: 5461
diff changeset
    11
*/
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    12
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    13
/* Simple program:  Loop, watching keystrokes
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    14
   Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to 
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    15
   pump the event loop and catch keystrokes.
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    16
*/
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    17
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    18
#include <stdio.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    19
#include <stdlib.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    20
#include <string.h>
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    21
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    22
#include "SDL.h"
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    23
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 493
diff changeset
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    25
static void
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    26
quit(int rc)
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 493
diff changeset
    27
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    28
    SDL_Quit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    29
    exit(rc);
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 493
diff changeset
    30
}
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 493
diff changeset
    31
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    32
static void
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    33
print_string(char **text, size_t *maxlen, const char *fmt, ...)
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    34
{
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    35
    int len;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    36
    va_list ap;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    37
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    38
    va_start(ap, fmt);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    39
    len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    40
    if (len > 0) {
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    41
        *text += len;
5879
cdb371a9b4a1 Fixed a compiler warning on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    42
        if ( ((size_t) len) < *maxlen ) {
cdb371a9b4a1 Fixed a compiler warning on Visual Studio.
Ryan C. Gordon <icculus@icculus.org>
parents: 5535
diff changeset
    43
            *maxlen -= (size_t) len;
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    44
        } else {
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    45
            *maxlen = 0;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    46
        }
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    47
    }
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    48
    va_end(ap);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    49
}
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    50
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    51
static void
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    52
print_modifiers(char **text, size_t *maxlen)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    53
{
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    54
    int mod;
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    55
    print_string(text, maxlen, " modifiers:");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    56
    mod = SDL_GetModState();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    57
    if (!mod) {
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    58
        print_string(text, maxlen, " (none)");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    59
        return;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    60
    }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    61
    if (mod & KMOD_LSHIFT)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    62
        print_string(text, maxlen, " LSHIFT");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    63
    if (mod & KMOD_RSHIFT)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    64
        print_string(text, maxlen, " RSHIFT");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    65
    if (mod & KMOD_LCTRL)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    66
        print_string(text, maxlen, " LCTRL");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    67
    if (mod & KMOD_RCTRL)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    68
        print_string(text, maxlen, " RCTRL");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    69
    if (mod & KMOD_LALT)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    70
        print_string(text, maxlen, " LALT");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    71
    if (mod & KMOD_RALT)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    72
        print_string(text, maxlen, " RALT");
2303
d87417504c75 First pass implementation of new SDL scancode concept, as discussed with
Sam Lantinga <slouken@libsdl.org>
parents: 2300
diff changeset
    73
    if (mod & KMOD_LGUI)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    74
        print_string(text, maxlen, " LGUI");
2303
d87417504c75 First pass implementation of new SDL scancode concept, as discussed with
Sam Lantinga <slouken@libsdl.org>
parents: 2300
diff changeset
    75
    if (mod & KMOD_RGUI)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    76
        print_string(text, maxlen, " RGUI");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    77
    if (mod & KMOD_NUM)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    78
        print_string(text, maxlen, " NUM");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    79
    if (mod & KMOD_CAPS)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    80
        print_string(text, maxlen, " CAPS");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    81
    if (mod & KMOD_MODE)
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    82
        print_string(text, maxlen, " MODE");
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    83
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    84
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    85
static void
5218
572a73d71b5f Sheena pointed out that "scancode" and "keysym" are single words and shouldn't be camel-cased.
Sam Lantinga <slouken@libsdl.org>
parents: 5081
diff changeset
    86
PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
    87
{
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    88
    char message[512];
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    89
    char *spot;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    90
    size_t left;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    91
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    92
    spot = message;
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    93
    left = sizeof(message);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    94
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    95
    /* Print the keycode, name and state */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
    96
    if (sym->sym) {
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    97
        print_string(&spot, &left,
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    98
                "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
    99
                pressed ? "pressed " : "released",
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   100
                sym->scancode,
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   101
                SDL_GetScancodeName(sym->scancode),
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   102
                sym->sym, SDL_GetKeyName(sym->sym));
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   103
    } else {
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   104
        print_string(&spot, &left,
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   105
                "Unknown Key (scancode %d = %s) %s ",
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   106
                sym->scancode,
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   107
                SDL_GetScancodeName(sym->scancode),
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   108
                pressed ? "pressed" : "released");
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   109
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   110
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   111
    /* Print the translated character, if one exists */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   112
    if (sym->unicode) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   113
        /* Is it a control-character? */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   114
        if (sym->unicode < ' ') {
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   115
            print_string(&spot, &left, " (^%c)", sym->unicode + '@');
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   116
        } else {
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   117
#ifdef UNICODE
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   118
            print_string(&spot, &left, " (%c)", sym->unicode);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   119
#else
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   120
            /* This is a Latin-1 program, so only show 8-bits */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   121
            if (!(sym->unicode & 0xFF00))
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   122
                print_string(&spot, &left, " (%c)", sym->unicode);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   123
            else
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   124
                print_string(&spot, &left, " (0x%X)", sym->unicode);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   125
#endif
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   126
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   127
    }
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   128
    print_modifiers(&spot, &left);
4560
95352c671a6e Added support for keyboard repeat (only tested on Windows so far)
Sam Lantinga <slouken@libsdl.org>
parents: 2315
diff changeset
   129
    if (repeat) {
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   130
        print_string(&spot, &left, " (repeat)");
4560
95352c671a6e Added support for keyboard repeat (only tested on Windows so far)
Sam Lantinga <slouken@libsdl.org>
parents: 2315
diff changeset
   131
    }
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   132
    SDL_Log("%s", message);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   133
}
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   134
2300
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   135
static void
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   136
PrintText(char *text)
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   137
{
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   138
    SDL_Log("Text: %s", text);
2300
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   139
}
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   140
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   141
int
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   142
main(int argc, char *argv[])
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   143
{
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   144
    SDL_Window *window;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   145
    SDL_Event event;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   146
    int done;
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   147
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   148
    /* Initialize SDL */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   149
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   150
        fprintf(stderr, "Couldn't initialize SDL: %s\n", SDL_GetError());
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   151
        return (1);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   152
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   153
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   154
    /* Set 640x480 video mode */
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   155
    window = SDL_CreateWindow("CheckKeys Test",
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   156
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   157
                              640, 480, 0);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   158
    if (!window) {
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   159
        fprintf(stderr, "Couldn't create 640x480 window: %s\n",
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   160
                SDL_GetError());
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   161
        quit(2);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   162
    }
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   163
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   164
#if __IPHONEOS__
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   165
    /* Creating the context creates the view, which we need to show keyboard */
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   166
    SDL_GL_CreateContext(window);
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   167
#endif
6392
fa7eb111f994 Fixed bug 1564 - SDL has no function to open a screen keyboard on Android.
Sam Lantinga <slouken@libsdl.org>
parents: 5879
diff changeset
   168
6654
2ecfb25be1e2 Synchronized the on-screen keyboard state with whether we are accepting text input.
Sam Lantinga <slouken@libsdl.org>
parents: 6392
diff changeset
   169
    SDL_StartTextInput();
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   170
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   171
    /* Watch keystrokes */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   172
    done = 0;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   173
    while (!done) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   174
        /* Check for events */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   175
        SDL_WaitEvent(&event);
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   176
        switch (event.type) {
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   177
        case SDL_KEYDOWN:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   178
        case SDL_KEYUP:
4560
95352c671a6e Added support for keyboard repeat (only tested on Windows so far)
Sam Lantinga <slouken@libsdl.org>
parents: 2315
diff changeset
   179
            PrintKey(&event.key.keysym, event.key.state, event.key.repeat);
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   180
            break;
2300
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   181
        case SDL_TEXTINPUT:
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   182
            PrintText(event.text.text);
c97ad1abe05b Minimal implementation of textinput events for x11. It only works for latin-1.
Bob Pendleton <bob@pendleton.com>
parents: 2295
diff changeset
   183
            break;
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   184
        case SDL_MOUSEBUTTONDOWN:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   185
            /* Any button press quits the app... */
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   186
        case SDL_QUIT:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   187
            done = 1;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   188
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   189
        default:
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   190
            break;
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   191
        }
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   192
    }
1151
be9c9c8f6d53 Removed atexit() from most of the test programs; atexit(SDL_Quit) isn't safe
Ryan C. Gordon <icculus@icculus.org>
parents: 493
diff changeset
   193
1895
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   194
    SDL_Quit();
c121d94672cb SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
Sam Lantinga <slouken@libsdl.org>
parents: 1253
diff changeset
   195
    return (0);
0
74212992fb08 Initial revision
Sam Lantinga <slouken@lokigames.com>
parents:
diff changeset
   196
}
5461
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   197
94f742ce580a Fixed bug 1163 (SDL_TEXTINPUT not being received on iPhoneOS)
Sam Lantinga <slouken@libsdl.org>
parents: 5218
diff changeset
   198
/* vi: set ts=4 sw=4 expandtab: */