Android: Allow back button on controllers to be bound

This commit is contained in:
Connor McLaughlin 2021-03-20 02:52:59 +10:00
parent 7e4453a7d4
commit 891b4642b7
2 changed files with 39 additions and 23 deletions

View File

@ -72,8 +72,8 @@ public class ControllerBindingDialog extends AlertDialog {
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (!EmulationSurfaceView.isBindableDevice(event.getDevice()) || !EmulationSurfaceView.isBindableKeyCode(event.getKeyCode())) {
return super.onKeyUp(keyCode, event);
if (!EmulationSurfaceView.isBindableDevice(event.getDevice()) || !EmulationSurfaceView.isBindableKeyEvent(event)) {
return super.onKeyDown(keyCode, event);
}
if (mType == ControllerBindingPreference.Type.BUTTON)
@ -81,7 +81,7 @@ public class ControllerBindingDialog extends AlertDialog {
else if (mType == ControllerBindingPreference.Type.VIBRATION)
mCurrentBinding = event.getDevice().getDescriptor();
else
return super.onKeyUp(keyCode, event);
return super.onKeyDown(keyCode, event);
updateMessage();
updateBinding();

View File

@ -35,46 +35,62 @@ public class EmulationSurfaceView extends SurfaceView {
((sources & InputDevice.SOURCE_CLASS_BUTTON) == InputDevice.SOURCE_CLASS_BUTTON);
}
public static boolean isGamepadDevice(InputDevice inputDevice) {
final int sources = (inputDevice != null) ? inputDevice.getSources() : 0;
return ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD);
}
public static boolean isJoystickMotionEvent(MotionEvent event) {
final int source = event.getSource();
return ((source & InputDevice.SOURCE_CLASS_JOYSTICK) == InputDevice.SOURCE_CLASS_JOYSTICK);
}
public static boolean isGamepadDevice(InputDevice inputDevice) {
final int sources = inputDevice.getSources();
return ((sources & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD);
}
public static boolean isBindableKeyCode(int keyCode) {
switch (keyCode) {
public static boolean isBindableKeyEvent(KeyEvent event) {
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_HOME:
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_POWER:
case KeyEvent.KEYCODE_CAMERA:
case KeyEvent.KEYCODE_CALL:
case KeyEvent.KEYCODE_ENDCALL:
case KeyEvent.KEYCODE_VOICE_ASSIST:
return false;
// We're okay if we get these from a gamepad.
return isGamepadDevice(event.getDevice());
default:
return true;
}
}
private static boolean isExternalKeyCode(int keyCode) {
private static boolean isSystemKeyCode(int keyCode) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_HOME:
case KeyEvent.KEYCODE_MENU:
case KeyEvent.KEYCODE_SOFT_RIGHT:
case KeyEvent.KEYCODE_HOME:
case KeyEvent.KEYCODE_BACK:
case KeyEvent.KEYCODE_CALL:
case KeyEvent.KEYCODE_ENDCALL:
case KeyEvent.KEYCODE_VOLUME_UP:
case KeyEvent.KEYCODE_VOLUME_DOWN:
case KeyEvent.KEYCODE_VOLUME_MUTE:
case KeyEvent.KEYCODE_MUTE:
case KeyEvent.KEYCODE_POWER:
case KeyEvent.KEYCODE_HEADSETHOOK:
case KeyEvent.KEYCODE_MEDIA_PLAY:
case KeyEvent.KEYCODE_MEDIA_PAUSE:
case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
case KeyEvent.KEYCODE_MEDIA_STOP:
case KeyEvent.KEYCODE_MEDIA_NEXT:
case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
case KeyEvent.KEYCODE_MEDIA_REWIND:
case KeyEvent.KEYCODE_MEDIA_RECORD:
case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
case KeyEvent.KEYCODE_CAMERA:
case KeyEvent.KEYCODE_CALL:
case KeyEvent.KEYCODE_ENDCALL:
case KeyEvent.KEYCODE_VOICE_ASSIST:
case KeyEvent.KEYCODE_FOCUS:
case KeyEvent.KEYCODE_SEARCH:
case KeyEvent.KEYCODE_BRIGHTNESS_DOWN:
case KeyEvent.KEYCODE_BRIGHTNESS_UP:
case KeyEvent.KEYCODE_MEDIA_AUDIO_TRACK:
case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_UP:
case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_DOWN:
case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_LEFT:
case KeyEvent.KEYCODE_SYSTEM_NAVIGATION_RIGHT:
return true;
default:
@ -208,7 +224,7 @@ public class EmulationSurfaceView extends SurfaceView {
hi.handleControllerButtonEvent(controllerIndex, keyCode, pressed);
// We don't want to eat external button events unless it's actually bound.
if (isExternalKeyCode(keyCode))
if (isSystemKeyCode(keyCode))
return (controllerIndex >= 0 && hi.hasControllerButtonBinding(controllerIndex, keyCode));
else
return true;