Looking for a better way to handle keyinput..

Hi there,

So, I am trying to implement a new method of handling KeyInput, do to gamestates and an entity handler.

What I have been using is this:
KeyInput class > KeyPressed(), KeyReleased() …

Game class > init() method > this.addKeyListener(new KeyInput());

How could I add keyinput that picks up key input from each gamestate? (Basically, pick up key input per class that gets rendered.)

Thanks!

Could you please post the relevant code?

Sure, but I don’t really have it being actively used, I removed it from my code.

Here is the concept.

Game class, “run” method includes init();
In the init() method, we have the following -
this.addKeyListener(new KeyInput());

The keyinput class looks as follows:

public KeyInput extends KeyAdapter(){

public KeyInput(){} //Constructor

public void keyPressed(KeyEvent e){
int key = e.getKeyCode();

  //For example
  if(key == KeyEvent.VK_ESCAPE) System.exit(1);

}

public void keyReleased(KeyEvent e){
int key = e.getKeyCode();

  //Again
  if(key == KeyEvent.VK_ESCAPE) System.exit(1);

}
}

Thats generally the code.

Why do you have the same code in keyPressed() and keyReleased()? If the key is pressed, then it will have to be released, so the code will be run twice.

Yes, I know. I just put it in there for example. Lol

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.

an interface called and it wants its switch statement back :point: