【问题标题】:identifying keyboard shortcut (Ctrl + W) press in chrome识别键盘快捷键(Ctrl + W)在chrome中按下
【发布时间】:2015-01-21 19:34:09
【问题描述】:

我正在尝试编写一个特定的脚本,当窗口关闭快捷键 (Ctrl + W/Ctrl + F4) 是按下。
这适用于 IE 和 Firefox,但对 Chrome 完全不起作用。
我正在使用下面的代码来做同样的事情。
如果有更好更合适的方法,请建议我。

var prevKey = ""
$(document).keydown(function (e) {//Enter and F5 button press
    if (e.keyCode == "116" || e.keyCode == "13") {
        window.onbeforeunload = null;
    }//Ctrl + w
    else if (e.ctrlKey == true && e.keyCode == "87") {//FOR CTRL + W
        alert("Ctrl + W");
        window.onbeforeunload = ConfirmLeave;
    }//(Ctrl or Alt) + F4
    else if ((e.ctrlKey == true || e.altKey == true) && e.keyCode == "115") {
        window.onbeforeunload = ConfirmLeave;
    }

【问题讨论】:

  • 你不能这样写if (e.keyCode == "116" || e.keyCode == "13") { window.onbeforeunload = null; } else {window.onbeforeunload = ConfirmLeave;} 吗?
  • 非常感谢......只是让事情变得复杂了一点,而不是直接的方法......再次感谢@CerlinBoss

标签: javascript jquery google-chrome browser keyboard-shortcuts


【解决方案1】:

尝试检查Window对象上的点击事件,检查keyCode是否等于W的keyCode,然后检查CTRL键是否被点击。 这部分 sn-p 已经为我在 FF 和 Chrome 上工作过。

$(window).keydown(function(e) {
    // check for W button
    if (e.which == 87) {
        // check if CTRL is clicked!
        if (e.ctrlKey) {
            alert('gg');
        }
    }
});

【讨论】:

    猜你喜欢
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2020-11-16
    • 2010-09-15
    • 1970-01-01
    • 1970-01-01
    • 2014-09-15
    • 1970-01-01
    相关资源
    最近更新 更多