How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?

前端 未结 8 1923
忘了有多久
忘了有多久 2020-11-27 04:03

Here is my code:

function pauseSound() {
    var pauseSound = document.getElementById(\"backgroundMusic\");
    pauseSound.pause(); 
}

I wo

8条回答
  •  感情败类
    2020-11-27 04:52

    an event handler for the document's keyup event seems like an appropriate solution

    // define a handler
    function doc_keyUp(e) {
    
        // this would test for whichever key is 40 and the ctrl key at the same time
        if (e.ctrlKey && e.keyCode == 40) {
            // call your function to do the thing
            pauseSound();
        }
    }
    // register the handler 
    document.addEventListener('keyup', doc_keyUp, false);
    

提交回复
热议问题