How to detect that Ctrl+R was pressed?

只愿长相守 提交于 2019-11-28 06:20:30
Alan Jackson

You have to use the keydown function to trap Ctrl characters. Here is my implementation of Ctrl+A:

    $(document).keydown(function(e) {
        if (e.keyCode == 65 && e.ctrlKey) {
            alert('ctrl A');
        }
    });

Ctrl-R is tougher because in most browsers, that is Reload Page, which means the javascript doesn't run, the page is refreshed.

Just a note as well, the keyCode value are different in the keydown/keyupup functions than in the keypress functions.

EDIT: Removed ctrl variable, forgot about ctrlKey

Gabe

Here is an entire list of keycodes that you can use.

Saurav S.

This is the code I'm using to disable refresh on IE and firefox (This works well for F5, Ctrl+F5 and Ctrl+R)

<script language="javascript" type="text/javascript">
    //this code handles the F5/Ctrl+F5/Ctrl+R
    document.onkeydown = checkKeycode
    function checkKeycode(e) {
        var keycode;
        if (window.event)
            keycode = window.event.keyCode;
        else if (e)
            keycode = e.which;

        // Mozilla firefox
        if ($.browser.mozilla) {
            if (keycode == 116 ||(e.ctrlKey && keycode == 82)) {
                if (e.preventDefault)
                {
                    e.preventDefault();
                    e.stopPropagation();
                }
            }
        } 
        // IE
        else if ($.browser.msie) {
            if (keycode == 116 || (window.event.ctrlKey && keycode == 82)) {
                window.event.returnValue = false;
                window.event.keyCode = 0;
                window.status = "Refresh is disabled";
            }
        }
    }
</script>

If you don't want to use useragent to detect what type of browser it is ($.browser uses navigator.userAgent to determine the platform), you can use

if('MozBoxSizing' in document.documentElement.style) - returns true for firefox

why aren't you using e.ctrlKey ?

 if (e.keyCode == 65 && e.ctrlKey) {
     alert('ctrl A');
 }

edit: here's an appropriate function to detect your ctrl-r keypress and stop the browser from reloading.

function keydown(e) {
    if (e.ctrlKey && e.keyCode == 82) {
        // 82 = r

        // TODO: your thing.

        if (e.preventDefault) {
            e.preventDefault();
        }
        else {
            return false;
        }
    }
}

i'm a jquery newbie, i think you'd do

$(document).keydown(keydown);

right?

There is a boolean property called ctrlKey that you should be able to use here...

$(document).keypress(function(e) { 
   alert("Ctrl is pressed: " + e.ctrlKey); 
}); 

Use event.key and modern JS!

$(document).keypress(function(event) {
    if (event.key === "r" && event.ctrlKey) {
        // Do something
    }
});

or without jQuery:

document.addEventListener("keypress", function onEvent(event) {
    if (event.key === "r" && event.ctrlKey) {
        // Do something better
    }
});

Mozilla Docs

Supported Browsers

Jithu
 $(document).ready(function () {
     $(document).keyup(function (e) {
         if (e.keyCode == 81 && e.ctrlKey) { //CTRL+Q
             alert("CTRL+Q");
         } else if (e.keyCode == 27) { //ESCAPE
             alert("escape");
         } else if (e.keyCode == 67 && e.altKey) { // ALT+C
           alert("ALT+C");
        }     
    });
});

key codes

Rahul Kumar

The Key Code for Ctrl key is 11.

$(document).keypress(function(e) { 


  alert("Ctrl is pressed: " + e.ctrlKey); 
}); 
@HostListener('window:keydown', ['$event'])
  keyEvent(event: KeyboardEvent) {

   if (event.ctrlKey && event.keyCode == 82)
    {

    }
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!