Implemented translation from Android keycodes to SDL scancodes
authorSam Lantinga <slouken@libsdl.org>
Wed, 12 Jan 2011 21:21:21 -0800
changeset 49933c9058f3ffe7
parent 4992 b12d76ef05e2
child 4994 e4ed74189d63
Implemented translation from Android keycodes to SDL scancodes
android-project/src/org/libsdl/app/SDLActivity.java
src/video/android/SDL_androidkeyboard.c
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 21:21:08 2011 -0800
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Wed Jan 12 21:21:21 2011 -0800
     1.3 @@ -313,12 +313,12 @@
     1.4      public boolean onKey(View  v, int keyCode, KeyEvent event) {
     1.5  
     1.6          if (event.getAction() == KeyEvent.ACTION_DOWN) {
     1.7 -            Log.v("SDL", "key down: " + keyCode);
     1.8 +            //Log.v("SDL", "key down: " + keyCode);
     1.9              SDLActivity.onNativeKeyDown(keyCode);
    1.10              return true;
    1.11          }
    1.12          else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.13 -            Log.v("SDL", "key up: " + keyCode);
    1.14 +            //Log.v("SDL", "key up: " + keyCode);
    1.15              SDLActivity.onNativeKeyUp(keyCode);
    1.16              return true;
    1.17          }
     2.1 --- a/src/video/android/SDL_androidkeyboard.c	Wed Jan 12 21:21:08 2011 -0800
     2.2 +++ b/src/video/android/SDL_androidkeyboard.c	Wed Jan 12 21:21:21 2011 -0800
     2.3 @@ -21,6 +21,8 @@
     2.4  */
     2.5  #include "SDL_config.h"
     2.6  
     2.7 +#include <android/log.h>
     2.8 +
     2.9  #include "../../events/SDL_events_c.h"
    2.10  
    2.11  #include "SDL_androidkeyboard.h"
    2.12 @@ -35,18 +37,144 @@
    2.13      SDL_SetKeymap(0, keymap, SDL_NUM_SCANCODES);
    2.14  }
    2.15  
    2.16 +static SDL_scancode Android_Keycodes[] = {
    2.17 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_UNKNOWN */
    2.18 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SOFT_LEFT */
    2.19 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SOFT_RIGHT */
    2.20 +    SDL_SCANCODE_AC_HOME, /* AKEYCODE_HOME */
    2.21 +    SDL_SCANCODE_AC_BACK, /* AKEYCODE_BACK */
    2.22 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_CALL */
    2.23 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_ENDCALL */
    2.24 +    SDL_SCANCODE_0, /* AKEYCODE_0 */
    2.25 +    SDL_SCANCODE_1, /* AKEYCODE_1 */
    2.26 +    SDL_SCANCODE_2, /* AKEYCODE_2 */
    2.27 +    SDL_SCANCODE_3, /* AKEYCODE_3 */
    2.28 +    SDL_SCANCODE_4, /* AKEYCODE_4 */
    2.29 +    SDL_SCANCODE_5, /* AKEYCODE_5 */
    2.30 +    SDL_SCANCODE_6, /* AKEYCODE_6 */
    2.31 +    SDL_SCANCODE_7, /* AKEYCODE_7 */
    2.32 +    SDL_SCANCODE_8, /* AKEYCODE_8 */
    2.33 +    SDL_SCANCODE_9, /* AKEYCODE_9 */
    2.34 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_STAR */
    2.35 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_POUND */
    2.36 +    SDL_SCANCODE_UP, /* AKEYCODE_DPAD_UP */
    2.37 +    SDL_SCANCODE_DOWN, /* AKEYCODE_DPAD_DOWN */
    2.38 +    SDL_SCANCODE_LEFT, /* AKEYCODE_DPAD_LEFT */
    2.39 +    SDL_SCANCODE_RIGHT, /* AKEYCODE_DPAD_RIGHT */
    2.40 +    SDL_SCANCODE_SELECT, /* AKEYCODE_DPAD_CENTER */
    2.41 +    SDL_SCANCODE_VOLUMEUP, /* AKEYCODE_VOLUME_UP */
    2.42 +    SDL_SCANCODE_VOLUMEDOWN, /* AKEYCODE_VOLUME_DOWN */
    2.43 +    SDL_SCANCODE_POWER, /* AKEYCODE_POWER */
    2.44 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_CAMERA */
    2.45 +    SDL_SCANCODE_CLEAR, /* AKEYCODE_CLEAR */
    2.46 +    SDL_SCANCODE_A, /* AKEYCODE_A */
    2.47 +    SDL_SCANCODE_B, /* AKEYCODE_B */
    2.48 +    SDL_SCANCODE_C, /* AKEYCODE_C */
    2.49 +    SDL_SCANCODE_D, /* AKEYCODE_D */
    2.50 +    SDL_SCANCODE_E, /* AKEYCODE_E */
    2.51 +    SDL_SCANCODE_F, /* AKEYCODE_F */
    2.52 +    SDL_SCANCODE_G, /* AKEYCODE_G */
    2.53 +    SDL_SCANCODE_H, /* AKEYCODE_H */
    2.54 +    SDL_SCANCODE_I, /* AKEYCODE_I */
    2.55 +    SDL_SCANCODE_J, /* AKEYCODE_J */
    2.56 +    SDL_SCANCODE_K, /* AKEYCODE_K */
    2.57 +    SDL_SCANCODE_L, /* AKEYCODE_L */
    2.58 +    SDL_SCANCODE_M, /* AKEYCODE_M */
    2.59 +    SDL_SCANCODE_N, /* AKEYCODE_N */
    2.60 +    SDL_SCANCODE_O, /* AKEYCODE_O */
    2.61 +    SDL_SCANCODE_P, /* AKEYCODE_P */
    2.62 +    SDL_SCANCODE_Q, /* AKEYCODE_Q */
    2.63 +    SDL_SCANCODE_R, /* AKEYCODE_R */
    2.64 +    SDL_SCANCODE_S, /* AKEYCODE_S */
    2.65 +    SDL_SCANCODE_T, /* AKEYCODE_T */
    2.66 +    SDL_SCANCODE_U, /* AKEYCODE_U */
    2.67 +    SDL_SCANCODE_V, /* AKEYCODE_V */
    2.68 +    SDL_SCANCODE_W, /* AKEYCODE_W */
    2.69 +    SDL_SCANCODE_X, /* AKEYCODE_X */
    2.70 +    SDL_SCANCODE_Y, /* AKEYCODE_Y */
    2.71 +    SDL_SCANCODE_Z, /* AKEYCODE_Z */
    2.72 +    SDL_SCANCODE_COMMA, /* AKEYCODE_COMMA */
    2.73 +    SDL_SCANCODE_PERIOD, /* AKEYCODE_PERIOD */
    2.74 +    SDL_SCANCODE_LALT, /* AKEYCODE_ALT_LEFT */
    2.75 +    SDL_SCANCODE_RALT, /* AKEYCODE_ALT_RIGHT */
    2.76 +    SDL_SCANCODE_LSHIFT, /* AKEYCODE_SHIFT_LEFT */
    2.77 +    SDL_SCANCODE_RSHIFT, /* AKEYCODE_SHIFT_RIGHT */
    2.78 +    SDL_SCANCODE_TAB, /* AKEYCODE_TAB */
    2.79 +    SDL_SCANCODE_SPACE, /* AKEYCODE_SPACE */
    2.80 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SYM */
    2.81 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_EXPLORER */
    2.82 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_ENVELOPE */
    2.83 +    SDL_SCANCODE_RETURN, /* AKEYCODE_ENTER */
    2.84 +    SDL_SCANCODE_DELETE, /* AKEYCODE_DEL */
    2.85 +    SDL_SCANCODE_GRAVE, /* AKEYCODE_GRAVE */
    2.86 +    SDL_SCANCODE_MINUS, /* AKEYCODE_MINUS */
    2.87 +    SDL_SCANCODE_EQUALS, /* AKEYCODE_EQUALS */
    2.88 +    SDL_SCANCODE_LEFTBRACKET, /* AKEYCODE_LEFT_BRACKET */
    2.89 +    SDL_SCANCODE_RIGHTBRACKET, /* AKEYCODE_RIGHT_BRACKET */
    2.90 +    SDL_SCANCODE_BACKSLASH, /* AKEYCODE_BACKSLASH */
    2.91 +    SDL_SCANCODE_SEMICOLON, /* AKEYCODE_SEMICOLON */
    2.92 +    SDL_SCANCODE_APOSTROPHE, /* AKEYCODE_APOSTROPHE */
    2.93 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SLASH */
    2.94 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_AT */
    2.95 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_NUM */
    2.96 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_HEADSETHOOK */
    2.97 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_FOCUS */
    2.98 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_PLUS */
    2.99 +    SDL_SCANCODE_MENU, /* AKEYCODE_MENU */
   2.100 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_NOTIFICATION */
   2.101 +    SDL_SCANCODE_AC_SEARCH, /* AKEYCODE_SEARCH */
   2.102 +    SDL_SCANCODE_AUDIOPLAY, /* AKEYCODE_MEDIA_PLAY_PAUSE */
   2.103 +    SDL_SCANCODE_AUDIOSTOP, /* AKEYCODE_MEDIA_STOP */
   2.104 +    SDL_SCANCODE_AUDIONEXT, /* AKEYCODE_MEDIA_NEXT */
   2.105 +    SDL_SCANCODE_AUDIOPREV, /* AKEYCODE_MEDIA_PREVIOUS */
   2.106 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_MEDIA_REWIND */
   2.107 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_MEDIA_FAST_FORWARD */
   2.108 +    SDL_SCANCODE_MUTE, /* AKEYCODE_MUTE */
   2.109 +    SDL_SCANCODE_PAGEUP, /* AKEYCODE_PAGE_UP */
   2.110 +    SDL_SCANCODE_PAGEDOWN, /* AKEYCODE_PAGE_DOWN */
   2.111 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_PICTSYMBOLS */
   2.112 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_SWITCH_CHARSET */
   2.113 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_A */
   2.114 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_B */
   2.115 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_C */
   2.116 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_X */
   2.117 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_Y */
   2.118 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_Z */
   2.119 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_L1 */
   2.120 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_R1 */
   2.121 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_L2 */
   2.122 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_R2 */
   2.123 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_THUMBL */
   2.124 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_THUMBR */
   2.125 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_START */
   2.126 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_SELECT */
   2.127 +    SDL_SCANCODE_UNKNOWN, /* AKEYCODE_BUTTON_MODE */
   2.128 +};
   2.129 +
   2.130 +static SDL_scancode
   2.131 +TranslateKeycode(int keycode)
   2.132 +{
   2.133 +    SDL_scancode scancode = SDL_SCANCODE_UNKNOWN;
   2.134 +
   2.135 +    if (keycode < SDL_arraysize(Android_Keycodes)) {
   2.136 +        scancode = Android_Keycodes[keycode];
   2.137 +    }
   2.138 +    if (scancode == SDL_SCANCODE_UNKNOWN) {
   2.139 +        __android_log_print(ANDROID_LOG_INFO, "SDL", "Unknown keycode %d", keycode);
   2.140 +    }
   2.141 +    return scancode;
   2.142 +}
   2.143 +
   2.144  int
   2.145  Android_OnKeyDown(int keycode)
   2.146  {
   2.147 -    /* FIXME: Need conversion from Android keycode to SDL scancode */
   2.148 -    return SDL_SendKeyboardKey(SDL_PRESSED, (SDL_scancode)keycode);
   2.149 +    return SDL_SendKeyboardKey(SDL_PRESSED, TranslateKeycode(keycode));
   2.150  }
   2.151  
   2.152  int
   2.153  Android_OnKeyUp(int keycode)
   2.154  {
   2.155 -    /* FIXME: Need conversion from Android keycode to SDL scancode */
   2.156 -    return SDL_SendKeyboardKey(SDL_RELEASED, (SDL_scancode)keycode);
   2.157 +    return SDL_SendKeyboardKey(SDL_RELEASED, TranslateKeycode(keycode));
   2.158  }
   2.159  
   2.160  /* vi: set ts=4 sw=4 expandtab: */