p5.js | Keyboard | key
The key variable in p5.js always contains the value of the key which is recently pressed. To get the proper capitalisation, it is best to use it within keyTyped(). Use the keyCode variable For non-ASCII keys.
Syntax:
key
Below program illustrates the key variable in p5.js:
Example-1:
let valueX; let valueY; function setup() { // Create Canvas of size 500*500 createCanvas(1000, 500); } function draw() { // set background color background(200); fill('green'); // set text and text size textSize(25); text( 'Press Any Key to change the text within circle' , 30, 30); // use of keyIsPressed Variable if (keyIsPressed) { // draw ellipse ellipse(mouseX, mouseY, 115, 115); fill('red'); text("Key Is Pressed", 100, 300); textSize(100); text(key, mouseX, mouseY); } else { rect(mouseX / 2, mouseY / 2, 300, 200); text(key, mouseX, mouseY); } } |
chevron_right
filter_none
Output:

Example-2:
let valueX; let valueY; function setup() { // Create Canvas of size 500*500 createCanvas(500, 500); } function draw() { // set background color background(200); fill('green'); // set text and text size textSize(100); text(key, height / 2, width / 2); if (keyIsPressed) { textSize(40); text("is pressed", height / 7, width - 50); } } |
chevron_right
filter_none
Output:

Reference: https://p5js.org/reference/#/p5/key
Recommended Posts:
- p5.js Keyboard | keyIsPressed
- p5.js | Keyboard | keyIsDown()
- How to access an object having spaces in the object's key using JavaScript ?
- How to move an element to left, right, up and down using arrow keys ?
- How to check a checkbox with jQuery?
- What is the Efficient way to insert a number into a sorted array of numbers?
- How to merge the duplicate value in multidimensional array in PHP ?
- How to Show and Hide div elements using Checkboxes ?
- How to get the first key name of a JavaScript object ?
- How to animate div width and height on mouse hover using jQuery ?
- JavaScript | Nested functions
- How to make horizontal scrollable in a bootstrap row?
- What is the difference between offsetHeight and clientHeight ?
- How to disable scroll to change number in <input type="number"> field using JavaScript/jQuery?
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.



