-1

I'm looking to introduce two consecutive keys (first one, then another) within a period of time and store the value of each keycode. The time period would be 3 seconds or more and the keys are numbers only.

In this code I've only got numbers from 1 to 9.

 @Override
    public boolean onKeyDown (int keyCode, KeyEvent event){
    event.getScanCode();
    switch (keyCode) {
        case KeyEvent.KEYCODE_BACK:
            break;
        case KeyEvent.KEYCODE_DPAD_UP:
            break;
        case KeyEvent.KEYCODE_DPAD_DOWN:              
            break;
        case KeyEvent.KEYCODE_DPAD_RIGHT:
            break;
        case KeyEvent.KEYCODE_DPAD_LEFT:
            break;
        case KeyEvent.KEYCODE_ENTER:
            super.onKeyDown(KeyEvent.KEYCODE_ENTER, event);
            break;
        default:
            //This is the part
       if (keyCode>7 && keyCode<17){
            indice_layouts[(keyCode - 8)].requestFocus();
        }else{
            Toast.makeText(this,"empty number", Toast.LENGTH_LONG).show();
        }
            break;
    }


        return true;
}

I'm looking to put first 1 and then 3 and finally get the 13.

Thanks in advance!

1 Answers1

0

You could check using class variables. Pseudocode below. It's a bit hacky, but it should work.

long timeButton1 = 0;
int keyCode1 = 0;
public boolean onKey(...) {
    ... 
    if(keyCode == KEYCODE_1) {
        timeButton1 = new Date().getTime();
        keycode1 = KEYCODE_1;
    }
    if(keyCode == KEYCODE_3 && new Date().getTime() - timeButton1 > 3000 && keycode1 == KEYCODE_1) {
        //Reset
        timeButton1 = 0;
        keycode1 = 0;
        //Do awesome stuff
        ...
    } 
}
Nick Felker
  • 11,536
  • 1
  • 21
  • 35