Opera preventDefault() on keydown event

前端 未结 1 1826
醉话见心
醉话见心 2021-01-13 08:36

I\'m trying to embed some keybindings in my webapp, and I\'m having hard times with Opera. I have this code:

window.onkeydown = function(e){
  var key = e.ke         


        
相关标签:
1条回答
  • 2021-01-13 09:12

    Opera doesn't support preventDefault on keydown, only on keypress.

    As you can see in this example, you should bind a separate keypress handler for Opera (adapted to your situation):

    var cancelKeypress = false;
    
    document.onkeydown = function(evt) {
        evt = evt || window.event;
        cancelKeypress = (evt.ctrlKey && evt.keyCode == 84);
        if (cancelKeypress) {
            return false;
        }
    };
    
    /* For Opera */
    document.onkeypress = function(evt) {
        if (cancelKeypress) {
            return false;
        }
    };
    
    0 讨论(0)
提交回复
热议问题