javascript - detect ctrl key pressed or up, keypress event doesn't trigger

前端 未结 3 939
再見小時候
再見小時候 2020-12-05 13:03

I see some similar questions here (like JavaScript: Check if CTRL button was pressed) but my problem is actually the event triggering. My js code:

    // Lis         


        
3条回答
  •  半阙折子戏
    2020-12-05 13:25

    Your event has a property named ctrlKey. You can check this to look if the key was pressed or not. See snippet below for more control like keys.

    function detectspecialkeys(e){
        var evtobj=window.event? event : e
        if (evtobj.altKey || evtobj.ctrlKey || evtobj.shiftKey)
            alert("you pressed one of the 'Alt', 'Ctrl', or 'Shift' keys")
    }
    document.onkeypress=detectspecialkeys
    

提交回复
热议问题