简体   繁体   English

当您想将重复的按键按下消息与javascript一起使用时,如果您按住按键但只想播放一次,该如何避免重复的按键按下消息?

[英]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 playing the oscillator when the mouse button is released. 它的作用是在鼠标单击名为synth的div时发出振荡器声音,然后在释放鼠标按钮时停止播放振荡器。

 synth.onmousedown= function () {
    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); 


    };



    synth.onmouseup = function ()    {  
    oscillator.disconnect(); 

    };

I then added the code below so that when a user presses a keyboard charachter it triggers the oscillator. 然后,我在下面添加了代码,以便当用户按下键盘字符时触发振荡器。 The problem is that when the key is held down it repeadedly plays and then won't stop when the keyboard character is released. 问题在于,按住该键时,它会反复播放,然后在释放键盘字符时不会停止。 I want to know if there are any libraries or code that can be used to circumvent this. 我想知道是否有任何库或代码可用于规避此问题。 The goal is to have the end result be like a mousedown/up effect but with keyboard movements triggering the sound.Thank you. 我们的目标是使最终结果像鼠标按下/向上移动一样,但是键盘移动会触发声音。谢谢。

$('body').keydown(function() {
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); 
});

$('body').keyup(function() {
oscillator.disconnect();

});

you could use underscore.js debounce if you need something fancier than the below option. 如果您需要比以下选项更高级的功能,则可以使用underscore.js反跳。

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;
});

This might be a workaround 这可能是一种解决方法

var keyIsDown = false;

$('body').keydown(function() {
    if(keyIsDown) {
       // key is already down
       return true;
    }

    keyIsDown = true;

    // do your keydown handler
}

$('body').keyup(function() {
    keyIsDown = false;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果在使用Redux时希望访问全局状态的一部分,该怎么用? - What do you use if you want to have access to a portion of the global state when you are using Redux? 如何规避多点触控? - How do you circumvent multi-touch? 如何使用JavaScript防止出现“您确定要离开此页面”消息时没有弹出窗口的窗口关闭? - How Do I Use JavaScript To Prevent A Window From Closing Without The Popup With The “Are You Sure You Want To Leave This Page” Message? ASP.NET MVC - 如果您不想将它们存储在/ Scripts中,您将把.js文件放在哪里? - ASP.NET MVC - Where do you put your .js files if you dont want to store them in /Scripts? 询问您是否确定要提交? JavaScript? - Ask if you are sure you want to submit? JavaScript? 您如何仅通过JavaScript播放视频? - How do you play a video through JavaScript only? JavaScript 中的逻辑运算符——你如何使用它们? - Logical operators in JavaScript — how do you use them? 是 res/req 关键字还是您可以随意命名它们? - Are res/req keywords or can you name them whatever you want to? 如何在 JavaScript 中播放音频文件? - How do you play audio files in JavaScript? 当您按下Enter键时,我希望我的表格继续进行 - I want my form to progress when you hit the enter key
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM