Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript keycodes for Alt, Ctrl, and Shift

$("#text").bind('keypress', function(e) {
    var code = (e.KeyCode ? e.keyCode : e.which);  
    alert(code);
}

It works fine for everything except Alt, Ctrl, or Shift.
But in all tutorials that i found it should echos 17, 18, 19

Why?

like image 237
Qiao Avatar asked Dec 20 '25 15:12

Qiao


1 Answers

use .keydown() or .keyup()

see it in action

jQuery(function($){
  var output = $('.output');
  $("#text1").bind('keypress', function(e) {
    var code = (e.KeyCode ? e.keyCode : e.which);  
    output.text(code);
  });
  $("#text2").bind('keydown', function(e) {
    var code = (e.KeyCode ? e.keyCode : e.which);  
    output.text(code);
  });
  $("#text3").bind('keyup', function(e) {
    var code = (e.KeyCode ? e.keyCode : e.which);  
    output.text(code);
  });
  
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
keypress <input type="text" id="text1"> <br>
keydown <input type="text" id="text2"> <br>
keyup <input type="text" id="text3">

<br><br>
Output: <span class="output"></span>
like image 178
Reigel Avatar answered Dec 23 '25 03:12

Reigel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!