p5.js | Keyboard keyCode

The keyCode variable in p5.js always contains the key code of the key that is recently pressed. This key code is recommended to be used when finding out if the user if pressing a modifier key, like the Shift, Contol, the Caps Lock key, or the Function keys.

Syntax: 

keyCode

The program below illustrates the keyCode variable in p5.js:
Example:
 

javascript

filter_none

edit
close

play_arrow

link
brightness_4
code

function setup() {
  createCanvas(400, 300);
}
  
function draw() {
  clear();
  textSize(18);
  fill("black");
  text("Press any key to see its keyCode", 60, 20);
  text("The name of the key pressed", 70, 60);
  
  textSize(52);
  fill("red");
  
  // Show the key that is reccently pressed
  text(key, 170, 120);
  textSize(18);
  fill("black");
    
  text("The keyCode of the key pressed", 60, 160);
  textSize(52);
  fill("red");
  
  // Show the key code of the key that is
  // reccently pressed
  text(keyCode, 160, 220);
}

chevron_right


Output:



keyCode-w-name

Reference: https://p5js.org/reference/#/p5/keyCode
Environment Setup: https://www.geeksforgeeks.org/p5-js-soundfile-object-installation-and-methods/

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

full-stack-img

My Personal Notes arrow_drop_up

Im listening

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.