Does anybody know if there is any predefined way to play a sound for every letter typed from keyboard into the HTML form?
For example: In a text field if I type Y, website says Y and so on.
Or, what would be the best way to do it?
What happens in your computer when you press a key on your keyboard? First, a switch beneath the key closes, and current flows into a small chip in a keyboard. Each key has a scan code number, which corresponds to its position on the keyboard. The keyboard transmits this number as binary data to the computer's CPU.
Visit Keyboard Checker and tap the key you want to test. If a key on the on-screen keyboard turns green, that means the keypress is being recognized however, the keyboard you see is NOT going to be an accurate representation of the keyboard you're using.
It's easy to play sound, and it's easy to add handlers to key press, but there is no predefined way to link the two operations so you'll have to type your own code.
1) act on key press
document.onkeydown = function() {
...
2 ) play sound
Add an audio element :
<audio id=alarm>
<source src=sound/zbluejay.wav>
</audio>
And execute it with
document.getElementById('alarm').play();
You could for example build a map linking keycodes to sound element ids :
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 may find keycodes here
You must have sound files for all letters and call them play on button press event with JavaScript.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With