i'm building a drum machine and i want to be able trigger drum sounds with specific keys on the keyboard. I added event listeners for keypress and keydown events but the sounds are not played when those keys are pressed. The current JavaScript code to do this is below(I also used the function in an input element and it triggered the sound successfully);
document.getElementById('drumpad1').addEventListener('keypress', tFunction(event));
function tFunction(event) {
var key = event.keyCode;
var char = String.fromCharCode(key);
var charUp = char.toUpperCase()
if (char === 'q' || charUp === 'Q') {
document.getElementById("beat").play();
}
}
I also have tried document.body.addEventListener()
didn't work.
please help