Mouse events are great for games – and if you’ve followed the ipad accelerometer tutorial you’ve even learned how to use the accelerometer in your games.  But in many games you may want to access the arrow keys or take text input from the gamers.  Fortunately Processing.js makes it easy to get the keyboard input onto the html5 canvas.

Similar to the mousedown function there is a keyboard event function that is called whenever a key is pressed:


void keyPressed(){
  println( keyCode );
  if(keyCode == LEFT){
    //Do something
  }

  if(keyCode == RIGHT){

  }

  if(keyCode == 'P'){

  }

  if(keyCode == 'S'){

  }
}

 

Here is a game implemented using keyboard input: (you may need to click it to give it focus)

 
         

 
View the Source






No comments yet