0

I am trying to detect the event for ctr l+e in java-script. My code is given below.I get alert for ctr l and e key action separately but when i put those two condition in the and logic i don't get any output. Any suggestion where i am doing wrong ?

 <script type="text/javascript">
        document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 17  && evt.keyCode == 69) {
        alert("ctrl-e is pressed");
          event.preventDefault(); 
    }
}
    </script>

rahman_akon18
  • 336
  • 3
  • 14
  • `evt.keyCode == 17 && evt.keyCode == 69` won't ever be true. A property or variable cannot have two different values at the same time. – Felix Kling Mar 09 '16 at 07:48

1 Answers1

1

You need to use e.ctrlKey to check if Ctrl is pressed. And onkeypress not, onkeydown. Check the updated code:

document.onkeypress = function(evt) {
  var e = evt || window.event;
    if (e.ctrlKey && e.keyCode == 69) {
      alert("ctrl-e is pressed");
      e.preventDefault(); 
    }
}
shershen
  • 9,875
  • 11
  • 39
  • 60