Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

keypress, ctrl+c (or some combo like that)

I'm trying to create shortcuts on the website I'm making. I know I can do it this way:

if(e.which == 17) isCtrl=true; if(e.which == 83 && isCtrl == true) {     alert('CTRL+S COMBO WAS PRESSED!')     //run code for CTRL+S -- ie, save!     e.preventDefault(); } 

But the example below is easier and less code, but it's not a combo keypress event:

$(document).keypress("c",function() {   alert("Just C was pressed.."); }); 

So I want to know if by using this second example, I could do something like:

$(document).keypress("ctrl+c",function() {   alert("Ctrl+C was pressed!!"); }); 

is this possible? I've tried it and it didn't work, what am I doing wrong?

like image 634
android.nick Avatar asked Jan 05 '11 12:01

android.nick


People also ask

How do you check if Ctrl C is pressed?

It indicates if Ctrl was pressed at the time of the event, like this: $(document). keypress("c",function(e) { if(e. ctrlKey) alert("Ctrl+C was pressed!!"); });

What is keypress function?

The keypress() method triggers the keypress event, or attaches a function to run when a keypress event occurs. The keypress event is similar to the keydown event. The event occurs when a button is pressed down. However, the keypress event is not fired for all keys (e.g. ALT, CTRL, SHIFT, ESC).


1 Answers

Another approach (no plugin needed) is to just use .ctrlKey property of the event object that gets passed in. It indicates if Ctrl was pressed at the time of the event, like this:

$(document).keypress("c",function(e) {   if(e.ctrlKey)     alert("Ctrl+C was pressed!!"); }); 
like image 125
Nick Craver Avatar answered Sep 28 '22 03:09

Nick Craver