I'm working on an Android app/semi-game (API 19-23, Java) and I used this website to help me handle controller input. Overall it looks pretty similar to what the website says for "Process Gamepad Button Presses":
public boolean processKeyEvent(int keyCode, String name) {
boolean handled = false;
switch (keyCode) {
case KeyEvent.KEYCODE_BUTTON_L1:
...
case KeyEvent.KEYCODE_BUTTON_R1:
...
case KeyEvent.KEYCODE_BUTTON_A:
...
case KeyEvent.KEYCODE_BUTTON_B:
...
case KeyEvent.KEYCODE_BUTTON_X:
...
case KeyEvent.KEYCODE_BUTTON_Y:
...
case KeyEvent.KEYCODE_BUTTON_START:
...
case KeyEvent.KEYCODE_BACK:
...
}
if(handled) {
return true;
}
return false;
}
and:
public boolean dispatchKeyEvent(android.view.KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
handled = script.processKeyEvent(event.getKeyCode(), event.getDevice().getName());
}
if (handled) {
return true;
} else {
return super.dispatchKeyEvent(event);
}
}
It does return the right keycodes (according to this list), so:
- L1: 102
- R1: 103
- A: 96
- B: 97
- X: 99
- Y: 100
- Start: 108
- Back: 4
The problem is that half of the buttons not only activate through their right case but also other ones, which then has side effects of course:
- B activates: X, Y, Start
- X: Y, Start
- Y: Start
- R1: A
- L1: R1, A
At first I thought that maybe the controller (SteelSeries Stratus XL for Android) had broken but I already checked with a second one (MogaPro Power) and I got the same results with it.
Has anyone else experienced this before? Any idea what I could try to fix this?
break;
statements? \$\endgroup\$