29

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 someone please help?

UPDATE

    ///this works
    $(document).keydown(function(e){
      if(e.keyCode==17){alert("control was pressed")};
 });

Next Question-- How do I link control key press and another key press to execute a function?

  if(e.keyCode==17){llCtrlPress=1};
   if(e.keyCode==97 && llCtrlPress=1){DO SOMETHING}
  ????????????

That seems like it would work fine but then how do I set llCtrlpress back to '0' on keyup?

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
sadmicrowave
  • 39,964
  • 34
  • 108
  • 180
  • @sadmicrowave - I screwed up :( It should have been 17. I've corrected it now. – karim79 May 04 '10 at 16:44
  • If you remove your original question, it won't be useful for future people, please leave your original question and then open a new question if you have another one. – Alan Jackson May 04 '10 at 17:18

11 Answers11

53

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

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Alan Jackson
  • 6,361
  • 2
  • 31
  • 32
5

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

Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Saurav S.
  • 177
  • 3
  • 3
  • 1
    The user didn't ask to disable F5, so your answer (While helpful) doesn't answer the question. – George Stocker Nov 14 '12 at 12:27
  • 1
    Also, it's horrifying that you're trying to disable fundamental browser functions; whatever problem you're trying to solve, this is not the right answer. – jrz Dec 18 '14 at 18:36
5

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

Marko K
  • 346
  • 3
  • 12
Gabe
  • 49,577
  • 28
  • 142
  • 181
2

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); 
}); 
Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
2

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?

lincolnk
  • 11,218
  • 4
  • 40
  • 61
1
 $(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

ata
  • 3,398
  • 5
  • 20
  • 31
Jithu
  • 11
  • 3
1

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

Community
  • 1
  • 1
Gibolt
  • 42,564
  • 15
  • 187
  • 127
0

The Key Code for Ctrl key is 11.

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


  alert("Ctrl is pressed: " + e.ctrlKey); 
}); 
Fabrizio
  • 7,603
  • 6
  • 44
  • 104
Rahul Kumar
  • 528
  • 1
  • 3
  • 19
0
  • Pure JavaScript and the KeyboardEvent.key
  • Support metaKey for MAC
  • Use Event.preventDefault() to prevent reload
window.addEventListener("keydown", (ev) => {
  if (ev.key === "r" && (ev.ctrlKey || ev.metaKey)) {
    ev.preventDefault(); // Prevent browser reload on CTRL+R
    console.log(ev.key);
  }
});
Roko C. Buljan
  • 196,159
  • 39
  • 305
  • 313
-1
@HostListener('window:keydown', ['$event'])
  keyEvent(event: KeyboardEvent) {

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

    }
  }
Murugan
  • 615
  • 5
  • 19
-1

We can also use Ascii code for finding the characters as well as the characters

$('html').keydown(function (e) {
     keydownfunc(e);
});
function keydownfunc(e) {
   if (e.ctrlKey && (e.key === "r" || e.key === "R")) {
       alert("ctrl+R");
   }
    }
  • 1
    Hi. This duplicates many other answers. If you agree, please just delete it. (And ASCII is not involved with JavaScipt and HTML at all. See [KeyboardEvent.key](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) ) If are interested in answering the newest JavaScript questions, try a [search](https://stackoverflow.com/questions/tagged/javascript?tab=Newest) or tag subscription. – Tom Blodget Sep 07 '19 at 14:12