Javascript Key Codes for Alt, Ctrl and Shift

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

It works great for everything except Alt, Ctrlor Shift.
But in all the tutorials that I found, there should be an echo 17, 18, 19

Why?

+3
source share
3 answers

use .keydown()or.keyup()

see him 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>
Run codeHide result
+4
source

Do you consider using the jquery.hotkeys extension that allows you to bind by specifying a string?

"C-A-q" would bind you to: 

Control+ Alt+ q.

+1
source

, console.log, . .

$("#textbox").keydown(function(e) {
    console.log("keydown: "+e.keyCode);
});

keyup keypress, , .

: JavaScript ;)

0
source

Source: https://habr.com/ru/post/1761848/


All Articles