1

Here is the Demo

I am trying with

$('.container').on('keyup', function(e){                     
    console.log(e.keyCode);
});

but its always the first key that is pressed!.

Note - I want the detection to work on keypress as I am validating the div on each keypress

Ibrahim Khan
  • 20,616
  • 7
  • 42
  • 55
Ajey
  • 7,924
  • 12
  • 62
  • 86

1 Answers1

3

You can use event.ctrlKey to check if the Controle-Key is pressed at the same time:

if(e.ctrlKey && e.keyCode == 65/*A*/) {
CoderPi
  • 12,985
  • 4
  • 34
  • 62
  • I did try that but it is always returning false – Ajey Jan 07 '16 at 13:33
  • @Ajey Works: http://jsbin.com/retayulisa/1/edit?html,js,console,output does it work for you like that as well? – CoderPi Jan 07 '16 at 13:36
  • No sir still does not work, checked on windows and mac, the ctrlKey is false. What am I doing wrong. :( – Ajey Jan 07 '16 at 13:39
  • @Ajey what Browser are you using. It should realy work when you are pressing controle and A and then release A first. – CoderPi Jan 07 '16 at 13:41
  • change the event to `keypress` for better because `ctrl` key never got up. – Jai Jan 07 '16 at 13:42
  • @Ajey yes I would also use keypress, do you need keyup? – CoderPi Jan 07 '16 at 13:43
  • using chrome 45, I am building a twitter like text validation so I need the logic to work on keyup rather than keypress – Ajey Jan 07 '16 at 13:44
  • This was triggering both if and else condition. So i modified it to `if((e.ctrlKey && e.keyCode == 65) || e.keyCode == 17 /*ctrl key*/) {` . this worked for me. – Milan Maharjan Dec 26 '18 at 10:05