Ok, so you are asking how to have different states have different inputs right?
I would have a main keyListener that would then send the events to the correct state.
// So each state would implement KeyListener
public abstract class State implements KeyListener
{
public void keyPressed(KeyEvent e){}
public void keyReleased(KeyEvent e){}
public void keyTyped(KeyEvent e){}
}
public enum EnumState
{
STATE_1, STATE_2, STATE_3 //etc... Your own states of course
}
Then your specific states would extend this main class and provide the specific funtionality
public class State1 extends State
{
public void keyPressed(KeyEvent e)
{
// Blah Blah... Specific state stuff here
}
public void keyReleased(KeyEvent e)
{
// Blah Blah... Specific state stuff here
}
}
You then have a main keyListener class that delegates everything
public class KeyInput implements KeyListener
{
EnumState currentState;
private State1 state1;
private State2 state2;
//etc...
public void keyPressed(KeyEvent e)
{
switch(currentState)
{
case EnumState.STATE_1:
state1.keyPressed(e);
break;
case EnumState.STATE_2:
state2.keyPressed(e);
break;
// etc...
}
}
public void keyReleased(KeyEvent e)
{
switch(currentState)
{
case EnumState.STATE_1:
state1.keyReleased(e);
break;
case EnumState.STATE_2:
state2.keyReleased(e);
break;
// etc...
}
}
public void keyTyped(KeyEvent e){}
}
You would then only add the KeyInput class with addKeyListener(); You should also probably turn each specific state into a singleton, because you probably won’t have more than one of each.