jQuery & jQuery UI Documentation

jQuery & jQuery UI

event.which

event.which Returns: Number

Description: For key or button events, this attribute indicates the specific button or key that was pressed.

  • version added: 1.1.3event.which

event.which normalizes event.keyCode and event.charCode. It is recommended to watch event.which for keyboard key input. For more detail, read about event.charCode on the MDC.

Example:

Log what key was depressed.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-1.7rc2.js"></script>
</head>
<body>
  
<input id="whichkey" value="type something">
<div id="log"></div>
<script>$('#whichkey').bind('keydown',function(e){ 
  $('#log').html(e.type + ': ' +  e.which );
});  </script>

</body>
</html>

Result:

"keydown" 74