I am new to android development, making a simple game and using onKeyDown(…..) function but it works only for 1 key at a time, how to handle 2 keys at a time means can i move right or left with continous firing.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
switch (keyCode){
case KeyEvent.KEYCODE_DPAD_RIGHT:
bottle_movx+=2;
break;
case KeyEvent.KEYCODE_DPAD_LEFT:
bottle_movx-=2;
break;
case KeyEvent.KEYCODE_DPAD_DOWN:
fire=bottle_movx;
firechk=true;
break;
}
invalidate();
return super.onKeyDown(keyCode, event);
}
EDIT: Perhaps this could be of some help?
How do I handle simultaneous key presses in Java?
Quote:
One way would be to keep track yourself of what keys are currently down.
When you get a keyPressed event, add the new key to the list; when you get a keyReleased event, remove the key from the list.
Then in your game loop, you can do actions based on what’s in the list of keys.
EDIT 2: I also found this link which could be useful:
How do I handle multiple key presses in a Java Applet?