Reference keyTyped()

keyTyped()

A function that's called once when keys with printable characters are pressed.

Declaring the function keyTyped() sets a code block to run once automatically when the user presses any key with a printable character such as a or 1. Modifier keys such as SHIFT, CONTROL, and the arrow keys will be ignored:

function keyTyped() {
  // Code to run.
}

The key and keyCode variables will be updated with the most recently released value when keyTyped() is called by p5.js:

function keyTyped() {
  // Check for the "c" character using key.
  if (key === 'c') {
    // Code to run.
  }

  // Check for "c" using keyCode.
  if (keyCode === 67) {
    // Code to run.
  }
}

The parameter, event, is optional. keyTyped() is always passed a KeyboardEvent object with properties that describe the key press event:

function keyReleased(event) {
  // Code to run that uses the event.
  console.log(event);
}

Note: Use the keyPressed() function and keyCode system variable to respond to modifier keys such as ALT.

Browsers may have default behaviors attached to various key events. To prevent any default behavior for this event, add return false; to the end of the function.

Examples

Syntax

keyTyped([event])

Parameters

event

optional KeyboardEvent callback argument.


Related References