Fixed bug 4702 - Android back button does not send SDL_KEYDOWN event

fallback when event.getSource() is SOURCE_UNKNOWN
This commit is contained in:
Sylvain Becker 2019-07-03 13:37:54 +02:00
parent 3fc447dfc9
commit f994da0ef0
1 changed files with 14 additions and 5 deletions

View File

@ -1799,6 +1799,10 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Key events // Key events
@Override @Override
public boolean onKey(View v, int keyCode, KeyEvent event) { public boolean onKey(View v, int keyCode, KeyEvent event) {
int deviceId = event.getDeviceId();
int source = event.getSource();
// Dispatch the different events depending on where they come from // Dispatch the different events depending on where they come from
// Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD // Some SOURCE_JOYSTICK, SOURCE_DPAD or SOURCE_GAMEPAD are also SOURCE_KEYBOARD
// So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD // So, we try to process them as JOYSTICK/DPAD/GAMEPAD events first, if that fails we try them as KEYBOARD
@ -1806,20 +1810,25 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Furthermore, it's possible a game controller has SOURCE_KEYBOARD and // Furthermore, it's possible a game controller has SOURCE_KEYBOARD and
// SOURCE_JOYSTICK, while its key events arrive from the keyboard source // SOURCE_JOYSTICK, while its key events arrive from the keyboard source
// So, retrieve the device itself and check all of its sources // So, retrieve the device itself and check all of its sources
if (SDLControllerManager.isDeviceSDLJoystick(event.getDeviceId())) { if (SDLControllerManager.isDeviceSDLJoystick(deviceId)) {
// Note that we process events with specific key codes here // Note that we process events with specific key codes here
if (event.getAction() == KeyEvent.ACTION_DOWN) { if (event.getAction() == KeyEvent.ACTION_DOWN) {
if (SDLControllerManager.onNativePadDown(event.getDeviceId(), keyCode) == 0) { if (SDLControllerManager.onNativePadDown(deviceId, keyCode) == 0) {
return true; return true;
} }
} else if (event.getAction() == KeyEvent.ACTION_UP) { } else if (event.getAction() == KeyEvent.ACTION_UP) {
if (SDLControllerManager.onNativePadUp(event.getDeviceId(), keyCode) == 0) { if (SDLControllerManager.onNativePadUp(deviceId, keyCode) == 0) {
return true; return true;
} }
} }
} }
if ((event.getSource() & InputDevice.SOURCE_KEYBOARD) != 0) { if (source == InputDevice.SOURCE_UNKNOWN) {
InputDevice device = InputDevice.getDevice(deviceId);
source = device.getSources();
}
if ((source & InputDevice.SOURCE_KEYBOARD) != 0) {
if (event.getAction() == KeyEvent.ACTION_DOWN) { if (event.getAction() == KeyEvent.ACTION_DOWN) {
//Log.v("SDL", "key down: " + keyCode); //Log.v("SDL", "key down: " + keyCode);
if (SDLActivity.isTextInputEvent(event)) { if (SDLActivity.isTextInputEvent(event)) {
@ -1835,7 +1844,7 @@ class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
} }
} }
if ((event.getSource() & InputDevice.SOURCE_MOUSE) != 0) { if ((source & InputDevice.SOURCE_MOUSE) != 0) {
// on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses // on some devices key events are sent for mouse BUTTON_BACK/FORWARD presses
// they are ignored here because sending them as mouse input to SDL is messy // they are ignored here because sending them as mouse input to SDL is messy
if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) { if ((keyCode == KeyEvent.KEYCODE_BACK) || (keyCode == KeyEvent.KEYCODE_FORWARD)) {