I need to play a drum according to the key pressed on my keyboard. The only things that I’ve done is make it play when I click with the mouse.
I tried to add the function that plays the sounds of the drum according with to key of the keyboard, but I didn’t manage it.
function playKey(idKey) {
const element = document.querySelector(idKey);
if (element) {
element.play();
} else {
console.error('element nof found')
}
}
const btns = document.querySelectorAll('.btn')
for (i = 0; i < btns.length; i++) {
const key = btns[i];
const keyCode = key.classList[1];
const idKey = `#btn-${keyCode}`;
key.onclick = function() {
playKey(idKey);
}
}