mijmajmoj
mijmajmoj

Reputation: 59

How can you use arrow keys as input?

I am trying to make a 2 player game so as a result I am passing each players controls through the player class's constructor. This works well for normal keys but 1 player uses the arrow keys. How do you do you use the arrow keys as inputs as it is throwing the error "Badly formed character constant (expecting quote, got E)".

My key listener:

void keyPressed(){
  for (Player p:g.players){
    p.checkforInputs(Character.toLowerCase(key));
  }
}

My input checker:

void checkforInputs(char k){
    if(k==moveLeftKey){...

Creating the player class and passing it the controls:(all arguments are char)

players.add(new Player('LEFT','RIGHT','UP','DOWN','<','>','m')); 

Upvotes: 1

Views: 1018

Answers (1)

Mady Daby
Mady Daby

Reputation: 1269

Example to detect special keys:

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
       //Do Something
    }
  }
}

Please see processing keyboard input docs for more details.

Upvotes: 2

Related Questions