# P5.js sound and keyTyped() or keyPressed()

**URL:** https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583
**Category:** Libraries
**Created:** [March 5, 2022, 11:09pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583 "2022-03-05T23:09:56Z")
**Posts on this page:** 6
**Page:** 2

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [March 6, 2022, 10:02pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/23 "2022-03-06T22:02:13Z")

</div>

I tried the last demo that I posted with one of the files on the website that you referred to; I changed the name of the Vinyl file to ‘keyA.mp3’, uploaded it to the ‘assets’ folder, preloaded it, and it plays when I press ‘a’ on my keyboard.

---

<div class="post-metadata">

### Author: ![Jess3](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jess3](https://discourse.processing.org/u/Jess3)
#### Post date: [March 6, 2022, 10:04pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/24 "2022-03-06T22:04:41Z")

</div>

yes! I tried this code and it finally works! thank you! I still need to figure out how to make it stop playing though, because when I click on A again it just play the same song over the one still playing. And I need to click on the rectangle before clicking A for it to work. Do you think it’s possible for me to change the rectangle to a better animation?? My original thought was to click on a key then a basic animation would play and the sound will play as well.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [March 6, 2022, 10:09pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/25 "2022-03-06T22:09:54Z")

</div>

[https://p5js.org/reference/#/libraries/p5.sound](https://p5js.org/reference/#/libraries/p5.sound)

Look at this reference and find a stop(). Put two buttons on your interface: 1) start 2) stop and hook them up accordingly. Instead of typing ‘a’ you’ll be pushing buttons for start/stop.

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [March 6, 2022, 10:11pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/26 "2022-03-06T22:11:15Z")

</div>

Just curious why it all of a sudden started working; what did you leave out?

---

<div class="post-metadata">

### Author: ![Jess3](https://avatars.discourse-cdn.com/v4/letter/j/d6d6ee/32.png) [@Jess3](https://discourse.processing.org/u/Jess3)
#### Post date: [March 6, 2022, 10:16pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/27 "2022-03-06T22:16:01Z")

</div>

oh but my project idea is to make keyboard keys as DPM (Drum Pad Machine), so when a key is pressed a sound will play, I need to find a way to make it play and stop. There are also so many start functions in the reference but okay I’ll find the suitable ones.  
And it suddenly works because I didn’t use let value = 0 in the beginning I thought it wouldn’t be necessary. I didn’t add this part.  
let value = 0;  
function draw() {  
fill(value);  
rect(25, 25, 50, 50);

I can’t add more replies so I’ll just edit my response here, yes I’ll keep that in mind and thank you for your help!

---

<div class="post-metadata">

### Author: ![svan](https://avatars.discourse-cdn.com/v4/letter/s/82dd89/32.png) [@svan](https://discourse.processing.org/u/svan)
#### Post date: [March 6, 2022, 10:31pm UTC](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583/28 "2022-03-06T22:31:29Z")

</div>

> I didn’t add this part

That’s why it’s important to show _ **all** _ of your code; it would have saved us both some time. Something to remember for future questions.

[Previous page](https://discourse.processing.org/t/p5-js-sound-and-keytyped-or-keypressed/35583.md?page=1)
