Home 12: Keyboard input
Content
Cancel

12: Keyboard input

To use the keyboard for input you can use different methods. To know which key has been pressed, you need a so called keycode, and you can find out which keycode belongs to which key if you go to www.keycode.info and just press the key.

You can check inside the draw-function with an if-statement if a specific key is pressed or not.

1
2
3
4
5
function draw() {
  if (keyIsDown(65)) {
    console.log("The key " + key + " is pressed");
  }
}

In this example, we check if the key with the keycode 65 is pressend, which is a lowercase a.

But we can also override functions that are called if any key has been pressed or released. Similar to the mouse-buttons.

1
2
3
4
5
6
7
function keyPressed() {
  // Do something here
}

function keyReleased() {
  // Do something else here
}
This post is licensed under CC BY 4.0 by the author.