How to detect that Ctrl+R was pressed?

后端 未结 10 875
逝去的感伤
逝去的感伤 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:18

    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

提交回复
热议问题