0
\$\begingroup\$

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?

\$\endgroup\$
  • 1
    \$\begingroup\$ Looking at the key codes, a code above seems to activate those below... Could you be missing break; statements? \$\endgroup\$ – Tyyppi_77 Sep 1 '16 at 16:10
  • \$\begingroup\$ @Tyyppi_77 facepalm I didn't have the ones for the five keys right at the end but in an "else" part. Thank you! \$\endgroup\$ – Neph Sep 1 '16 at 16:54
7
\$\begingroup\$

After a quick investigation it looks like you are missing some break; statements from your cases which results in flow-trough.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.