处理浏览器的“ctrl+s"按键事件

我试图为基于浏览器的应用程序实现 CTRL+S 功能.我进行了搜索,并在以下问题中遇到了两个脚本

I was trying to implement the CTRL+S feature for a browser based application. I made a search and came across two scripts in the following to questions

捕获 CTRL+S 的最佳跨浏览器方法jQuery?
Ctrl+S preventDefault 在 Chrome 中

但是,当我尝试实现它时,它工作了,但我仍然得到默认浏览器保存对话框/窗口.

However, when I tried to implement it, it worked but, I still get the default browser save dialog box/window.

我的代码:对于 shortcut.js:

 shortcut.add("Ctrl+S",function() {
     alert("Hi there!");
 },
 {
     'type':'keydown',
     'propagate':false,
     'target':document
});

jQuery 热键.js:

jQuery hotkeys.js:

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

我相信 e.preventDefault(); 应该可以解决问题,但由于某种原因它不起作用.我哪里错了.对不起,如果很简单,还在学习jJvascript.

I believe e.preventDefault(); should do the trick, but for some reason it doesn't work. Where am I going wrong.Sorry if it is simple, still learning jJvascript.

推荐答案

这只是为我使用的问题添加不同的实现.改编自 SO 答案.也适用于 MAC

This is to just add a different implementation to the question used by me. Adapted from a SO answer.Also,works for MAC

 document.addEventListener("keydown", function(e) {
      if (e.keyCode == 83 && (navigator.platform.match("Mac") ? e.metaKey : e.ctrlKey))      {
        e.preventDefault();
        //your implementation or function calls
      }
    }, false);

相关文章