How to detect that Ctrl+R was pressed?

后端 未结 10 853
逝去的感伤
逝去的感伤 2020-12-08 14:27

I\'m coding a function in jquery that executes if Ctrl+R is pressed but I can\'t seem to find out what the left and right ctrl keycodes are... Can some

10条回答
  •  粉色の甜心
    2020-12-08 15:11

    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?

提交回复
热议问题