1

im using this code which i found here, on a stackoverflow post for keypress combination:

$(document).keypress("c",function(e) {
    if(e.ctrlKey) {
        alert('Combined');
    }
});

The thing is, i'm not sure what is the meaning of the "c", but this code will alert only on Ctrl+z, and i've tried to replace that "c", but still it only works on Ctrl+z.

This is where i found that code: jquery: keypress, ctrl+c (or some combo like that)

Why is that?

Community
  • 1
  • 1
Guy Aston
  • 169
  • 1
  • 2
  • 13

2 Answers2

1

there "c" doesn't do anything you can remove it. it alerts only on CTRL+Z because in if condition is e.ctrlKey if you remove it and write like this

$(document).keypress(function(e) {
    alert('Combined')
});

it alerts on every keypress

kaxi1993
  • 4,535
  • 4
  • 29
  • 47
  • The purpose of this code is to alert on Ctrl+c press. this is where i found this code: http://stackoverflow.com/questions/4604057/jquery-keypress-ctrlc-or-some-combo-like-that – Guy Aston Nov 09 '14 at 10:05
1

this code will alert only on Ctrl+z, and i've tried to replace that "c", but still it only works on Ctrl+z

That's not true. This code works on any combination Ctrl+[key].

Demo

So your issue cannot be reproduced.

Regarding to "c" as the first argument of .keypress method: According to manual the first argument ("c" in your case), if present, is passed to the event handler as property data of Event object. So you can access it by e.data notation in your event handler (see Demo).

hindmost
  • 7,125
  • 3
  • 27
  • 39
  • Ok well i have tried many buttons, and so far only ctrl+z and Ctrl+q worked but i guess its because other combinations are browser shortcuts (even though some combinations didnt do anything). thanks – Guy Aston Nov 09 '14 at 10:10