Key input handler variations

Hi, I"m developing my first game right now and I have an in-game menu which is drawn onto the screen, my question is that when the menu is displaying, the UP key for example will have a different functionality then when playing the actual game, is there a better way for doing this than the following?


	private class KeyInputHandler extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			if(showMenu) {
				if(e.getKeyCode() == KeyEvent.VK_DOWN) {
					changeSelectedItem(1);
				}
				// ...
			} else if(gameRunning){
				if(e.getKeyCode() == KeyEvent.VK_RIGHT) {
					cycleAircrafts(1);
				}
                                // ...
			}
		}
	}

Thanks.

Yes. The usual way. Change the key-state flags and respond to their state in the main loop.

There you could then for example call the tick() method for the menu or the game or whatever and do your stuff accordingly to the state of those flags.

static int [] controls = new int[0xFF];
[...]
public void keyPressed(KeyEvent ke){
	controls[ke.getKeyCode()&0xff] = 1;
}
public void keyReleased(KeyEvent ke){
	controls[ke.getKeyCode()&0xff] = 0;
}
public void keyTyped(KeyEvent ke){}

In the loop you check the state like:
px+=controls[KeyEvent.VK_RIGHT]-controls[KeyEvent.VK_LEFT]*SOMETHING;

You can also use booleans instead of ints if you like.

I see…thanks oNyx