It is easy to play sound, and it is easy to add handlers to keystrokes, but there is no predefined way to link the two operations, so you will need to enter your own code.
1) act upon pressing a key
document.onkeydown = function() { ...
2) sound reproduction
Add audio element:
<audio id=alarm> <source src=sound/zbluejay.wav> </audio>
And execute it with
document.getElementById('alarm').play();
You can, for example, build a map linking key codes with identifiers of sound elements:
var sounds = { 88 : 'alarm', // key 'x' ... }; document.onkeydown = function(e) { var soundId = sounds[e.keyCode]; if (soundId) document.getElementById(soundId).play(); else console.log("key not mapped : code is", e.keyCode); }
Yoy can find the key codes here
Denys seguret
source share