7 – KEYBOARD INPUT

 

 

BUILT-IN METHOD

 

Here is how we check if the right arrow key is down:

 

   Gdx.input.isKeyPressed(Input.Keys.RIGHT)

 

Here is how we check if the A key is down:

 

   Gdx.input.isKeyPressed(Input.Keys.A)

 

Here is how we check if the SPACE key is down:

 

   Gdx.input.isKeyPressed(Input.Keys.SPACE)

 

All three method calls above return true if the respective key is down at the time of the call. Otherwise, they return false.

 

FINDING THE KEYWORDS

 

From the examples above, we can easily figure out how to check if the arrow keys or all the letters are down.  But what about all the other keys?

 

Thankfully Eclipse provides a useful way to find out all options.  You simply type in everything up to and including the final dot and then hit CONTROL-SPACE to see all possible options.  This will show the keyword representing each key.  See the image below.

 

 

EXAMPLE – CHECKING FOR WASD & ARROWS

 

public class PongGame extends ApplicationAdapter

{

   private ShapeRenderer renderer;

   private boolean spacePressed;

   private boolean leftPressed;

   private boolean rightPressed;

   private boolean upPressed;

   private boolean downPressed;

   private boolean aPressed;

   private boolean wPressed;

   private boolean sPressed;

   private boolean dPressed;

 

   @Override

   public void create ()

   {

       renderer = new ShapeRenderer();

       spacePressed = false;

       leftPressed = false;

       rightPressed = false;

       upPressed = false;

       downPressed = false;

       aPressed = false;

       wPressed = false;

       sPressed = false;

       dPressed = false;

   }

 

   @Override

   public void render()

   {

        //Update

        rightPressed = Gdx.input.isKeyPressed(Input.Keys.RIGHT);

        leftPressed = Gdx.input.isKeyPressed(Input.Keys.LEFT);

        downPressed = Gdx.input.isKeyPressed(Input.Keys.DOWN);

        upPressed = Gdx.input.isKeyPressed(Input.Keys.UP);

        spacePressed = Gdx.input.isKeyPressed(Input.Keys.SPACE);

        aPressed = Gdx.input.isKeyPressed(Input.Keys.A);

        dPressed = Gdx.input.isKeyPressed(Input.Keys.D);

        sPressed = Gdx.input.isKeyPressed(Input.Keys.S);

        wPressed = Gdx.input.isKeyPressed(Input.Keys.W);

          

        //Clear the screen

        Gdx.gl.glClearColor(0, 0, 0, 1);

        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

 

        //Render circle in new location.

      renderer.begin(ShapeType.Filled);

      renderer.setColor(Color.BLUE);

     

      //RENDER LEFT BUTTON

      if (leftPressed || aPressed)

      {

        renderer.setColor(Color.RED);

      }      

      else

      {

        renderer.setColor(Color.BLUE); 

      }

      renderer.rect(10,10,200,200);  //left

     

      //RENDER DOWN BUTTON

      if (downPressed || sPressed)

      {

        renderer.setColor(Color.RED);

      }      

      else

      {

        renderer.setColor(Color.BLUE); 

      }

      renderer.rect(220,10,200,200); //down

     

      //RENDER RIGHT BUTTON

      if (rightPressed || dPressed)

      {

        renderer.setColor(Color.RED);

      }      

      else

      {

        renderer.setColor(Color.BLUE); 

      }

      renderer.rect(430,10,200,200); //right

 

      //RENDER UP BUTTON

      if (upPressed || wPressed)

      {

        renderer.setColor(Color.RED);

      }      

      else

      {

        renderer.setColor(Color.BLUE); 

      }      

      renderer.rect(220,220,200,200); //top

     

      //RENDER SPACE BUTTON

      if (spacePressed)

      {

        renderer.setColor(Color.RED);

      }      

      else

      {

        renderer.setColor(Color.BLUE); 

      }    

      renderer.rect(10, 430, 620, 40); //space

     

      renderer.end();

   }

}

The above application has four squares that represent either the arrow keys or the W, A, S and D keys.  The top rectangle represents the spacebar.  While one of the keys is pressed, the corresponding shape turns red.