diff --git a/01 - JavaScript Drum Kit/index-START.html b/01 - JavaScript Drum Kit/index-START.html index 4070d32767..58c004d307 100644 --- a/01 - JavaScript Drum Kit/index-START.html +++ b/01 - JavaScript Drum Kit/index-START.html @@ -57,9 +57,34 @@ - + function keyDownListener(e) { + + // Check if there exists an audio tag with the data-key that matches the + // triggered key. + const key = document.querySelector(`div.key[data-key="${e.keyCode}"]`); + const audio = document.querySelector(`audio[data-key="${e.keyCode}"]`); + + if (!audio) { return; } + // Only try to play if it exists, because audio will be null otherwise. + audio.currentTime = 0; + audio.play(); + + + key.classList.toggle("playing") + + const keys = document.querySelectorAll('div.key'); + keys.forEach(key => key.addEventListener("transitionend",removeTransition)); + + } + + window.addEventListener('keydown', keyDownListener); +
-