I tried to do this code:
$(input).keyup(function (e)
{
alert(e.shiftKey);
});
But every character that I write shows "false", how can I do that?
I tried to do this code:
$(input).keyup(function (e)
{
alert(e.shiftKey);
});
But every character that I write shows "false", how can I do that?
Sadly this is not possible with the event API — both shift keys return keyCode 16, both ctrl keys return 17 — they're indistinguishable!
Sorry for the bad news :(
Your problem is using keyup()
. By the time the event fires the shift key was released and is not more active. Try using keydown()
or keypress()
.
Try
$("input").keydown(function (e)
{
if(e.shiftKey && e.ctrlKey){ alert("shift + crtl Pressed");}
});
this works fine
The follow will be triggered while both keys are down:
$(input).keydown(function (e) {
if (e.shiftKey && e.ctrlKey) {
// This code would run if both the control and shift are down.
}
});
However, it will continue to be triggered for as long as they are down, see the jQuery example by holding down both keys in the input box.
I thought jQuery's .keypress() would be able to let you know when they were pressed rather than just held, but it appears that it only gets called on character keys a-z, 0-1, ?!>~#
, etc.