src/video/android/SDL_androidkeyboard.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 21:21:21 -0800
changeset 4993 3c9058f3ffe7
parent 4980 d9fdff945ec9
child 5081 25d4feb7c127
permissions -rw-r--r--
Implemented translation from Android keycodes to SDL scancodes
slouken@4980
     1
/*
slouken@4980
     2
    SDL - Simple DirectMedia Layer
slouken@4980
     3
    Copyright (C) 1997-2010 Sam Lantinga
slouken@4980
     4
slouken@4980
     5
    This library is free software; you can redistribute it and/or
slouken@4980
     6
    modify it under the terms of the GNU Lesser General Public
slouken@4980
     7
    License as published by the Free Software Foundation; either
slouken@4980
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@4980
     9
slouken@4980
    10
    This library is distributed in the hope that it will be useful,
slouken@4980
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@4980
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@4980
    13
    Lesser General Public License for more details.
slouken@4980
    14
slouken@4980
    15
    You should have received a copy of the GNU Lesser General Public
slouken@4980
    16
    License along with this library; if not, write to the Free Software
slouken@4980
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@4980
    18
slouken@4980
    19
    Sam Lantinga
slouken@4980
    20
    slouken@libsdl.org
slouken@4980
    21
*/
slouken@4980
    22
#include "SDL_config.h"
slouken@4980
    23
slouken@4993
    24
#include <android/log.h>
slouken@4993
    25
slouken@4980
    26
#include "../../events/SDL_events_c.h"
slouken@4980
    27
slouken@4980
    28
#include "SDL_androidkeyboard.h"
slouken@4980
    29
slouken@4980
    30
slouken@4980
    31
void Android_InitKeyboard()
slouken@4980
    32
{
slouken@4980
    33
    SDLKey keymap[SDL_NUM_SCANCODES];
slouken@4980
    34
slouken@4980
    35
    /* Add default scancode to key mapping */
slouken@4980
    36
    SDL_GetDefaultKeymap(keymap);
slouken@4980
    37
    SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
slouken@4980
    38
}
slouken@4980
    39
slouken@4993
    40
static SDL_scancode Android_Keycodes[] = {
slouken@4993
    41
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_UNKNOWN */
slouken@4993
    42
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SOFT_LEFT */
slouken@4993
    43
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SOFT_RIGHT */
slouken@4993
    44
    SDL_SCANCODE_AC_HOME, /* AKEYCODE_HOME */
slouken@4993
    45
    SDL_SCANCODE_AC_BACK, /* AKEYCODE_BACK */
slouken@4993
    46
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_CALL */
slouken@4993
    47
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_ENDCALL */
slouken@4993
    48
    SDL_SCANCODE_0, /* AKEYCODE_0 */
slouken@4993
    49
    SDL_SCANCODE_1, /* AKEYCODE_1 */
slouken@4993
    50
    SDL_SCANCODE_2, /* AKEYCODE_2 */
slouken@4993
    51
    SDL_SCANCODE_3, /* AKEYCODE_3 */
slouken@4993
    52
    SDL_SCANCODE_4, /* AKEYCODE_4 */
slouken@4993
    53
    SDL_SCANCODE_5, /* AKEYCODE_5 */
slouken@4993
    54
    SDL_SCANCODE_6, /* AKEYCODE_6 */
slouken@4993
    55
    SDL_SCANCODE_7, /* AKEYCODE_7 */
slouken@4993
    56
    SDL_SCANCODE_8, /* AKEYCODE_8 */
slouken@4993
    57
    SDL_SCANCODE_9, /* AKEYCODE_9 */
slouken@4993
    58
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_STAR */
slouken@4993
    59
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_POUND */
slouken@4993
    60
    SDL_SCANCODE_UP, /* AKEYCODE_DPAD_UP */
slouken@4993
    61
    SDL_SCANCODE_DOWN, /* AKEYCODE_DPAD_DOWN */
slouken@4993
    62
    SDL_SCANCODE_LEFT, /* AKEYCODE_DPAD_LEFT */
slouken@4993
    63
    SDL_SCANCODE_RIGHT, /* AKEYCODE_DPAD_RIGHT */
slouken@4993
    64
    SDL_SCANCODE_SELECT, /* AKEYCODE_DPAD_CENTER */
slouken@4993
    65
    SDL_SCANCODE_VOLUMEUP, /* AKEYCODE_VOLUME_UP */
slouken@4993
    66
    SDL_SCANCODE_VOLUMEDOWN, /* AKEYCODE_VOLUME_DOWN */
slouken@4993
    67
    SDL_SCANCODE_POWER, /* AKEYCODE_POWER */
slouken@4993
    68
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_CAMERA */
slouken@4993
    69
    SDL_SCANCODE_CLEAR, /* AKEYCODE_CLEAR */
slouken@4993
    70
    SDL_SCANCODE_A, /* AKEYCODE_A */
slouken@4993
    71
    SDL_SCANCODE_B, /* AKEYCODE_B */
slouken@4993
    72
    SDL_SCANCODE_C, /* AKEYCODE_C */
slouken@4993
    73
    SDL_SCANCODE_D, /* AKEYCODE_D */
slouken@4993
    74
    SDL_SCANCODE_E, /* AKEYCODE_E */
slouken@4993
    75
    SDL_SCANCODE_F, /* AKEYCODE_F */
slouken@4993
    76
    SDL_SCANCODE_G, /* AKEYCODE_G */
slouken@4993
    77
    SDL_SCANCODE_H, /* AKEYCODE_H */
slouken@4993
    78
    SDL_SCANCODE_I, /* AKEYCODE_I */
slouken@4993
    79
    SDL_SCANCODE_J, /* AKEYCODE_J */
slouken@4993
    80
    SDL_SCANCODE_K, /* AKEYCODE_K */
slouken@4993
    81
    SDL_SCANCODE_L, /* AKEYCODE_L */
slouken@4993
    82
    SDL_SCANCODE_M, /* AKEYCODE_M */
slouken@4993
    83
    SDL_SCANCODE_N, /* AKEYCODE_N */
slouken@4993
    84
    SDL_SCANCODE_O, /* AKEYCODE_O */
slouken@4993
    85
    SDL_SCANCODE_P, /* AKEYCODE_P */
slouken@4993
    86
    SDL_SCANCODE_Q, /* AKEYCODE_Q */
slouken@4993
    87
    SDL_SCANCODE_R, /* AKEYCODE_R */
slouken@4993
    88
    SDL_SCANCODE_S, /* AKEYCODE_S */
slouken@4993
    89
    SDL_SCANCODE_T, /* AKEYCODE_T */
slouken@4993
    90
    SDL_SCANCODE_U, /* AKEYCODE_U */
slouken@4993
    91
    SDL_SCANCODE_V, /* AKEYCODE_V */
slouken@4993
    92
    SDL_SCANCODE_W, /* AKEYCODE_W */
slouken@4993
    93
    SDL_SCANCODE_X, /* AKEYCODE_X */
slouken@4993
    94
    SDL_SCANCODE_Y, /* AKEYCODE_Y */
slouken@4993
    95
    SDL_SCANCODE_Z, /* AKEYCODE_Z */
slouken@4993
    96
    SDL_SCANCODE_COMMA, /* AKEYCODE_COMMA */
slouken@4993
    97
    SDL_SCANCODE_PERIOD, /* AKEYCODE_PERIOD */
slouken@4993
    98
    SDL_SCANCODE_LALT, /* AKEYCODE_ALT_LEFT */
slouken@4993
    99
    SDL_SCANCODE_RALT, /* AKEYCODE_ALT_RIGHT */
slouken@4993
   100
    SDL_SCANCODE_LSHIFT, /* AKEYCODE_SHIFT_LEFT */
slouken@4993
   101
    SDL_SCANCODE_RSHIFT, /* AKEYCODE_SHIFT_RIGHT */
slouken@4993
   102
    SDL_SCANCODE_TAB, /* AKEYCODE_TAB */
slouken@4993
   103
    SDL_SCANCODE_SPACE, /* AKEYCODE_SPACE */
slouken@4993
   104
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SYM */
slouken@4993
   105
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_EXPLORER */
slouken@4993
   106
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_ENVELOPE */
slouken@4993
   107
    SDL_SCANCODE_RETURN, /* AKEYCODE_ENTER */
slouken@4993
   108
    SDL_SCANCODE_DELETE, /* AKEYCODE_DEL */
slouken@4993
   109
    SDL_SCANCODE_GRAVE, /* AKEYCODE_GRAVE */
slouken@4993
   110
    SDL_SCANCODE_MINUS, /* AKEYCODE_MINUS */
slouken@4993
   111
    SDL_SCANCODE_EQUALS, /* AKEYCODE_EQUALS */
slouken@4993
   112
    SDL_SCANCODE_LEFTBRACKET, /* AKEYCODE_LEFT_BRACKET */
slouken@4993
   113
    SDL_SCANCODE_RIGHTBRACKET, /* AKEYCODE_RIGHT_BRACKET */
slouken@4993
   114
    SDL_SCANCODE_BACKSLASH, /* AKEYCODE_BACKSLASH */
slouken@4993
   115
    SDL_SCANCODE_SEMICOLON, /* AKEYCODE_SEMICOLON */
slouken@4993
   116
    SDL_SCANCODE_APOSTROPHE, /* AKEYCODE_APOSTROPHE */
slouken@4993
   117
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SLASH */
slouken@4993
   118
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_AT */
slouken@4993
   119
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_NUM */
slouken@4993
   120
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_HEADSETHOOK */
slouken@4993
   121
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_FOCUS */
slouken@4993
   122
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_PLUS */
slouken@4993
   123
    SDL_SCANCODE_MENU, /* AKEYCODE_MENU */
slouken@4993
   124
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_NOTIFICATION */
slouken@4993
   125
    SDL_SCANCODE_AC_SEARCH, /* AKEYCODE_SEARCH */
slouken@4993
   126
    SDL_SCANCODE_AUDIOPLAY, /* AKEYCODE_MEDIA_PLAY_PAUSE */
slouken@4993
   127
    SDL_SCANCODE_AUDIOSTOP, /* AKEYCODE_MEDIA_STOP */
slouken@4993
   128
    SDL_SCANCODE_AUDIONEXT, /* AKEYCODE_MEDIA_NEXT */
slouken@4993
   129
    SDL_SCANCODE_AUDIOPREV, /* AKEYCODE_MEDIA_PREVIOUS */
slouken@4993
   130
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_MEDIA_REWIND */
slouken@4993
   131
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_MEDIA_FAST_FORWARD */
slouken@4993
   132
    SDL_SCANCODE_MUTE, /* AKEYCODE_MUTE */
slouken@4993
   133
    SDL_SCANCODE_PAGEUP, /* AKEYCODE_PAGE_UP */
slouken@4993
   134
    SDL_SCANCODE_PAGEDOWN, /* AKEYCODE_PAGE_DOWN */
slouken@4993
   135
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_PICTSYMBOLS */
slouken@4993
   136
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SWITCH_CHARSET */
slouken@4993
   137
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_A */
slouken@4993
   138
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_B */
slouken@4993
   139
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_C */
slouken@4993
   140
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_X */
slouken@4993
   141
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_Y */
slouken@4993
   142
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_Z */
slouken@4993
   143
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_L1 */
slouken@4993
   144
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_R1 */
slouken@4993
   145
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_L2 */
slouken@4993
   146
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_R2 */
slouken@4993
   147
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_THUMBL */
slouken@4993
   148
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_THUMBR */
slouken@4993
   149
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_START */
slouken@4993
   150
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_SELECT */
slouken@4993
   151
    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_MODE */
slouken@4993
   152
};
slouken@4993
   153
slouken@4993
   154
static SDL_scancode
slouken@4993
   155
TranslateKeycode(int keycode)
slouken@4993
   156
{
slouken@4993
   157
    SDL_scancode scancode = SDL_SCANCODE_UNKNOWN;
slouken@4993
   158
slouken@4993
   159
    if (keycode < SDL_arraysize(Android_Keycodes)) {
slouken@4993
   160
        scancode = Android_Keycodes[keycode];
slouken@4993
   161
    }
slouken@4993
   162
    if (scancode == SDL_SCANCODE_UNKNOWN) {
slouken@4993
   163
        __android_log_print(ANDROID_LOG_INFO, "SDL", "Unknown keycode %d", keycode);
slouken@4993
   164
    }
slouken@4993
   165
    return scancode;
slouken@4993
   166
}
slouken@4993
   167
slouken@4980
   168
int
slouken@4980
   169
Android_OnKeyDown(int keycode)
slouken@4980
   170
{
slouken@4993
   171
    return SDL_SendKeyboardKey(SDL_PRESSED, TranslateKeycode(keycode));
slouken@4980
   172
}
slouken@4980
   173
slouken@4980
   174
int
slouken@4980
   175
Android_OnKeyUp(int keycode)
slouken@4980
   176
{
slouken@4993
   177
    return SDL_SendKeyboardKey(SDL_RELEASED, TranslateKeycode(keycode));
slouken@4980
   178
}
slouken@4980
   179
slouken@4980
   180
/* vi: set ts=4 sw=4 expandtab: */