How to detect that Ctrl+R was pressed?

后端 未结 10 854
逝去的感伤
逝去的感伤 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条回答
  •  Happy的楠姐
    2020-12-08 14:53

    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

提交回复
热议问题