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

前端 未结 8 1937
忘了有多久
忘了有多久 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:46

    //For single key: Short cut key for 'Z'
    document.onkeypress = function (e) {
        var evt = window.event || e;
        switch (evt.keyCode) {
            case 90:  
                // Call your method Here
                break;
        }
    }
    
    //For combine keys like Alt+P
    document.onkeyup = function (e) {
        var evt = window.event || e;   
            if (evt.keyCode == 80 && evt.altKey) {
                // Call Your method here   
            }
        }
    }
        //ensure if short cut keys are case sensitive.
        //    If its not case sensitive then
        //check with the evt.keyCode values for both upper case and lower case. ......
    

提交回复
热议问题