p5.js | keyTyped() Function

The keyTyped() function is invoked every time when a key is pressed, except for action keys like the directional arrow keys, Ctrl, Shift, Alt, Backspace and the Delete key. The most recently typed key is stored in the ‘key’ variable.

Note that, holding down a key may cause multiple keyTyped() calls. This is due to how the Operating System handles keypresses and depends on how the computer is configured. A browser may have its own default behavior attached to various keys which can be prevented by adding “return false” to the end of the method.

Syntax:

keyTyped()

Parameters: This method does not accept any parameters.

Below example illustrates the keyTyped() function in p5.js:



Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(600, 200);
  textSize(20);
  text("Press any key to display it on the screen", 10, 20);
  text("Any of the action keys would not be recognized", 10, 40);
}
  
function keyTyped() {
  clear();
  textSize(20);
  text("Press any key to display it on the screen", 10, 20);
  text("Any of the action keys would not be recognized", 10, 40);
  textSize(100);
  text(key, 100, 150);
}

chevron_right


Output:
display-typed

Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

Reference: https://p5js.org/reference/#/p5/keyTyped

full-stack-img

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.