admin管理员组

文章数量:1026925

$("#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?

$("#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?

Share Improve this question edited Nov 17, 2012 at 9:46 Be Brave Be Like Ukraine 7,7353 gold badges45 silver badges68 bronze badges asked Aug 27, 2010 at 2:42 QiaoQiao 17k32 gold badges96 silver badges120 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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./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>

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

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

Control + Alt + q.

It would probably be better to use console.log to find them. You can also use just one textbox easily for this.

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

And do the same for keyup and keypress to see better how it all works.

Side note: JavaScript sure is hard to write on a phone ;)

$("#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?

$("#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?

Share Improve this question edited Nov 17, 2012 at 9:46 Be Brave Be Like Ukraine 7,7353 gold badges45 silver badges68 bronze badges asked Aug 27, 2010 at 2:42 QiaoQiao 17k32 gold badges96 silver badges120 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

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./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>

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

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

Control + Alt + q.

It would probably be better to use console.log to find them. You can also use just one textbox easily for this.

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

And do the same for keyup and keypress to see better how it all works.

Side note: JavaScript sure is hard to write on a phone ;)

本文标签: jqueryJavascript keycodes for AltCtrland ShiftStack Overflow