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

Chris提出了一个问题:How can I add a JavaScript keyboard shortcut to an existing JavaScript Function?,或许与您遇到的问题类似。

回答者lincolnk给出了该问题的处理方式:

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

希望本文对你有帮助,欢迎支持JavaScript中文网


公众号

欢迎 JavaScript 爱好者