Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keydown has a delay

I'm using

    document.addEventListener('keydown', function(e) {
  if (e.keyCode == 65) {
    notedo.play()
  }
});

which essentially just plays a sound on keypress, however I'm trying to create sampler and there's a delay pressing a key. Anyway I can get around this? as well as being able to play multiple keys at once?

I am also using this in my HTML side:

<div class="black key" data-note="G#3" onclick="notedo.play()"></div>

So when clicking on my div element above it plays the notedo variable in javascript. I'm trying to find a way to implement key pressing to play the sound as well.

like image 650
Connor Taumoepeau Avatar asked Sep 10 '26 12:09

Connor Taumoepeau


1 Answers

Found my issue which I think was getting confused with delay.

I was actually trying to play the same audio immediately after I press the same button.

I ended up using this!

document.addEventListener('keydown', function(e) {
  if (e.keyCode == 65) {
    notedo.play()
  }
  if (e.keyCode == 65) {
    notedo.play();
    notedo.currentTime = 0
  }
});
like image 177
Connor Taumoepeau Avatar answered Sep 12 '26 00:09

Connor Taumoepeau



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!