How to Disable the CTRL+P using javascript or Jquery?

后端 未结 11 1090
一生所求
一生所求 2020-12-31 06:17

Here I tried to disable the Ctrl+P but it doesn\'t get me alert and also it shows the print options

jQuery(document).bind(\"keyup keydo         


        
11条回答
  •  时光取名叫无心
    2020-12-31 06:26

    After much testings on various browsers, it is easier to intercept the keys when they are down (not pressed) because some of this "App integrated keys" are difficult to intercept with the "keypress" event.

    I came up with this script that is sort of cross browser compatible (I didn't test for Microsoft's IE). Notice that the browsers return different codes for some keys. In my case I wanted to prevent Ctrl+P.

    The key "P" on chrome is seen as e.keyCode == 80, on opera, it is e.charCode == 16, while on firefox it is e.charCode == 112

    $(document).on('keydown', function(e) {
        if((e.ctrlKey || e.metaKey) && (e.key == "p" || e.charCode == 16 || e.charCode == 112 || e.keyCode == 80) ){
            alert("Please use the Print PDF button below for a better rendering on the document");
            e.cancelBubble = true;
            e.preventDefault();
    
            e.stopImmediatePropagation();
        }  
    });
    

    I used jQuery.

提交回复
热议问题