[Android] Improve handling of keyboard, dpad and gamepad events
authorGabriel Jacobo <gabomdq@gmail.com>
Mon, 11 Nov 2013 10:49:48 -0300
changeset 7957 5d9563c4f4b6
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
--- a/android-project/src/org/libsdl/app/SDLActivity.java	Mon Nov 11 10:15:35 2013 -0300
+++ b/android-project/src/org/libsdl/app/SDLActivity.java	Mon Nov 11 10:49:48 2013 -0300
@@ -600,7 +600,22 @@
     @Override
     public boolean onKey(View  v, int keyCode, KeyEvent event) {
         // Dispatch the different events depending on where they come from
-        if(event.getSource() == InputDevice.SOURCE_KEYBOARD) {
+        // Some SOURCE_DPAD or SOURCE_GAMEPAD events appear to also be marked as SOURCE_KEYBOARD
+        // So, to avoid problems, we process DPAD or GAMEPAD events first.
+        
+        if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
+                   (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
+            int id = SDLActivity.getJoyId( event.getDeviceId() );
+            if (id != -1) {
+                if (event.getAction() == KeyEvent.ACTION_DOWN) {
+                    SDLActivity.onNativePadDown(id, keyCode);
+                } else if (event.getAction() == KeyEvent.ACTION_UP) {
+                    SDLActivity.onNativePadUp(id, keyCode);
+                }
+            }
+            return true;
+        }
+        else if( (event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
             if (event.getAction() == KeyEvent.ACTION_DOWN) {
                 //Log.v("SDL", "key down: " + keyCode);
                 SDLActivity.onNativeKeyDown(keyCode);
@@ -611,17 +626,6 @@
                 SDLActivity.onNativeKeyUp(keyCode);
                 return true;
             }
-        } else if ( (event.getSource() & 0x00000401) != 0 || /* API 12: SOURCE_GAMEPAD */
-                   (event.getSource() & InputDevice.SOURCE_DPAD) != 0 ) {
-            int id = SDLActivity.getJoyId( event.getDeviceId() );
-            if (id != -1) {
-                if (event.getAction() == KeyEvent.ACTION_DOWN) {
-                    SDLActivity.onNativePadDown(id, keyCode);
-                } else if (event.getAction() == KeyEvent.ACTION_UP) {
-                    SDLActivity.onNativePadUp(id, keyCode);
-                }
-            }
-            return true;
         }
         
         return false;