Javascript Ctrl+S 在 Chrome 中防止默认

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11000826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:57:35  来源:igfitidea点击:

Ctrl+S preventDefault in Chrome

javascriptjquerygoogle-chromepreventdefault

提问by zupa

I Want to catch Ctrl+Sin Chrome, and prevent the default browser behavior to save the page. How?

我想在 Chrome 中捕获Ctrl+ S,并阻止默认浏览器行为来保存页面。如何?

(Just posting the question & answer as I was after this for a pretty long time and didn't find a solution)

(只是发布问题和答案,因为我在这之后很长一段时间没有找到解决方案)

回答by zupa

As far as I can see, the secret sauce is, that Ctrl+Sdoes NOT fire the keypress event, only the keydown event.

据我所知,秘诀是,Ctrl+S不会触发 keypress 事件,只会触发 keydown 事件。

Using jQuery.hotkeys:

使用jQuery.hotkeys

$(document).bind('keydown', 'ctrl+s', function(e) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
});

Only with jQuery:

仅使用 jQuery:

$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    alert('Ctrl+S');
    return false;
  }
});

Edit 2012.12.17- jQuery.hotkeys says

编辑 2012.12.17- jQuery.hotkeys 说

Hotkeys aren't tracked if you're inside of an input element (unless you explicitly bind the hotkey directly to the input). This helps to avoid conflict with normal user typing.

如果您在输入元素内,则不会跟踪热键(除非您明确将热键直接绑定到输入)。这有助于避免与普通用户键入发生冲突。

回答by BumbleB2na

"Borrowed" from Overriding control+s (save functionality) in browser

浏览器中的 Overriding control+s(保存功能)“借用”

document.addEventListener("keydown", function(e) {
  if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey)) {
    e.preventDefault();
    alert('captured');
  }
}, false);

回答by Latha

document.onkeydown = function (e) {
    e = e || window.event;//Get event
    if (e.ctrlKey) {
        var c = e.which || e.keyCode;//Get key code
        switch (c) {
            case 83://Block Ctrl+S
                e.preventDefault();     
                e.stopPropagation();
            break;
        }
    }
};
   

回答by suraj kumar

All in one solution to prevent data

防止数据泄露的多合一解决方案

// disable right click
$(function() {
    $(this).bind("contextmenu", function(e) {
        e.preventDefault();
    });
}); 

// Prevent F12      
$(document).keydown(function (event) {
    if (event.keyCode == 123) { // Prevent F12
        return false;
    } else if (event.ctrlKey && event.shiftKey && event.keyCode == 73) { // Prevent Ctrl+Shift+I        
        return false;
    }
});

//stop copy of content
function killCopy(e){
    return false
}
function reEnable(){
    return true
}
document.onselectstart=new Function ("return false")
    if (window.sidebar){
    document.onmousedown=killCopy
    document.onclick=reEnable
}

// prevent ctrl + s
$(document).bind('keydown', function(e) {
  if(e.ctrlKey && (e.which == 83)) {
    e.preventDefault();
    return false;
  }
});