'How to use the draw and keyTyped() function at the same time?

I want to create a code where when I click a key on the keyboard it will play music and draw a rectangle. I have managed to add music using the keyTyped() function but I am not sure how to add a rectangle to the canvas.

How to use the draw and keyTyped() function at the same time?



Solution 1:[1]

Something like this:

function setup() {
  createCanvas(400, 400);
  background("white");
}

function keyTyped(){
  
  //play sound
  
  fill("black");
  
  rect(50, 50, 100, 100);
  
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 KoderM