简体   繁体   English

识别键盘快捷键(Ctrl + W)在Chrome中按

[英]identifying keyboard shortcut (Ctrl + W) press in chrome

I am trying to write a certain script that will trigger when the window close shortcut ( Ctrl + W/Ctrl + F4 ) is pressed. 我正在尝试编写某个脚本,该脚本将在按下窗口关闭快捷方式( Ctrl + W / Ctrl + F4 )时触发。
This works fine for IE as well as Firefox but doesn't work at all for Chrome. 这对于IE和Firefox都适用,但对于Chrome根本不起作用。
I am using the below code for doing the same. 我正在使用以下代码执行相同的操作。
Please suggest me if there is a better and more proper way of doing it. 如果有更好更好的方法,请提出建议。

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;
    }

Try to check for click event on Window object and check if the keyCode is equal to keyCode of W , then check if the CTRL key is clicked. 尝试检查Window对象上的click事件,并检查keyCode是否等于W的 keyCode,然后检查是否单击了CTRL键。 This part of snippet has worked on FF and Chrome for me. 这段代码对我来说适用于FF和Chrome。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM