[Android] Improve handling of keyboard, dpad and gamepad events
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 11 Nov 2013 10:49:48 -0300
changeset 79575d9563c4f4b6
parent 7956 965b7e09c5a8
child 7958 b8f431cd7819
[Android] Improve handling of keyboard, dpad and gamepad events

Thanks Dimitris Zenios for the report!
android-project/src/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Nov 11 10:15:35 2013 -0300
     1.2 +++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Nov 11 10:49:48 2013 -0300
     1.3 @@ -600,7 +600,22 @@
     1.4      @Override
     1.5      public boolean onKey(View  v, int keyCode, KeyEvent event) {
     1.6          // Dispatch the different events depending on where they come from
     1.7 -        if(event.getSource() == InputDevice.SOURCE_KEYBOARD) {
     1.8 +        // Some SOURCE_DPAD or SOURCE_GAMEPAD events appear to also be marked as SOURCE_KEYBOARD
     1.9 +        // So, to avoid problems, we process DPAD or GAMEPAD events first.
    1.10 +        
    1.11 +        if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
    1.12 +                   (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
    1.13 +            int id = SDLActivity.getJoyId( event.getDeviceId() );
    1.14 +            if (id != -1) {
    1.15 +                if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.16 +                    SDLActivity.onNativePadDown(id, keyCode);
    1.17 +                } else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.18 +                    SDLActivity.onNativePadUp(id, keyCode);
    1.19 +                }
    1.20 +            }
    1.21 +            return true;
    1.22 +        }
    1.23 +        else if( (event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
    1.24              if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.25                  //Log.v("SDL", "key down: " + keyCode);
    1.26                  SDLActivity.onNativeKeyDown(keyCode);
    1.27 @@ -611,17 +626,6 @@
    1.28                  SDLActivity.onNativeKeyUp(keyCode);
    1.29                  return true;
    1.30              }
    1.31 -        } else if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
    1.32 -                   (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
    1.33 -            int id = SDLActivity.getJoyId( event.getDeviceId() );
    1.34 -            if (id != -1) {
    1.35 -                if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.36 -                    SDLActivity.onNativePadDown(id, keyCode);
    1.37 -                } else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.38 -                    SDLActivity.onNativePadUp(id, keyCode);
    1.39 -                }
    1.40 -            }
    1.41 -            return true;
    1.42          }
    1.43          
    1.44          return false;