Fixed bug 4702 - Android back button does not send SDL_KEYDOWN event
authorSylvain Becker <sylvain.becker@gmail.com>
Wed, 03 Jul 2019 13:37:54 +0200
changeset 12926b5cd5e1e4440
parent 12925 c6c6e400a007
child 12927 abb47c384db3
Fixed bug 4702 - Android back button does not send SDL_KEYDOWN event
fallback when event.getSource() is SOURCE_UNKNOWN
android-project/app/src/main/java/org/libsdl/app/SDLActivity.java
     1.1 --- a/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Wed Jul 03 02:37:15 2019 -0700
     1.2 +++ b/android-project/app/src/main/java/org/libsdl/app/SDLActivity.java	Wed Jul 03 13:37:54 2019 +0200
     1.3 @@ -1799,6 +1799,10 @@
     1.4      // Key events
     1.5      @Override
     1.6      public boolean onKey(View  v, int keyCode, KeyEvent event) {
     1.7 +
     1.8 +        int deviceId = event.getDeviceId();
     1.9 +        int source = event.getSource();
    1.10 +
    1.11          // Dispatch the different events depending on where they come from
    1.12          // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
    1.13          // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
    1.14 @@ -1806,20 +1810,25 @@
    1.15          // Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
    1.16          // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
    1.17          // So, retrieve the device itself and check all of its sources
    1.18 -        if (SDLControllerManager.isDeviceSDLJoystick(event.getDeviceId())) {
    1.19 +        if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
    1.20              // Note that we process events with specific key codes here
    1.21              if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.22 -                if (SDLControllerManager.onNativePadDown(event.getDeviceId(), keyCode) == 0) {
    1.23 +                if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
    1.24                      return true;
    1.25                  }
    1.26              } else if (event.getAction() == KeyEvent.ACTION_UP) {
    1.27 -                if (SDLControllerManager.onNativePadUp(event.getDeviceId(), keyCode) == 0) {
    1.28 +                if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
    1.29                      return true;
    1.30                  }
    1.31              }
    1.32          }
    1.33  
    1.34 -        if ((event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) {
    1.35 +        if (source == InputDevice.SOURCE_UNKNOWN) {
    1.36 +            InputDevice device = InputDevice.getDevice(deviceId);
    1.37 +            source = device.getSources();
    1.38 +        }
    1.39 +
    1.40 +        if ((source & InputDevice.SOURCE_KEYBOARD) != 0) {
    1.41              if (event.getAction() == KeyEvent.ACTION_DOWN) {
    1.42                  //Log.v("SDL", "key down: " + keyCode);
    1.43                  if (SDLActivity.isTextInputEvent(event)) {
    1.44 @@ -1835,7 +1844,7 @@
    1.45              }
    1.46          }
    1.47  
    1.48 -        if ((event.getSource() & InputDevice.SOURCE_MOUSE) != 0) {
    1.49 +        if ((source & InputDevice.SOURCE_MOUSE) != 0) {
    1.50              // on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
    1.51              // they are ignored here because sending them as mouse input to SDL is messy
    1.52              if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {