How Do You Circumvent Repeated Keydown Messages When You Want To Use Them With Javascript Where You Hold The Key Down But Want It Only To Play Once?
I have a piece of code that looks something like below and it works fine. What it does is it launches an oscillator sound when the mouse clicks on a div named synth.It then stops p
Solution 1:
you could use underscore.js debounce if you need something fancier than the below option.
var keydown = false;
$('body').keydown(function() {
if(!keydown){
oscillator = context.createOscillator(), // Creates the oscillator
oscillator.type = synthWaveform.value;
oscillator.frequency.value = pitchInput.value;
oscillator.connect(context.destination); // Connects it to output
oscillator.noteOn(0);
keydown = true;
}
});
$('body').keyup(function() {
oscillator.disconnect();
keydown = false;
});
Solution 2:
This might be a workaround
var keyIsDown = false;
$('body').keydown(function() {
if(keyIsDown) {
// key is already downreturntrue;
}
keyIsDown = true;
// do your keydown handler
}
$('body').keyup(function() {
keyIsDown = false;
}
Post a Comment for "How Do You Circumvent Repeated Keydown Messages When You Want To Use Them With Javascript Where You Hold The Key Down But Want It Only To Play Once?"