How to get the pressed key code using jQuery ?

Getting key code in jQuery

event.keyCode

event.keyCode returns the Unicode value of a non-character key in a KeyPress events.

Example jQuery code to get pressed key code

<html>
   <head>
      <title>The jQuery Example - To get key code</title>

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>		
      <script type = "text/javascript">
        function get_key_code(event) {
          var code = event.keyCode;
          $('span').text("Key code is: " + code);
          $('span').css('color', 'blue');
        }

      </script>		
   </head>	
   <body>
   <p>Test Section</p>
<p>Please enter the key to get pressed key code, 
<input type="text" onkeydown="get_key_code(event);"/></p>
<p><span></span></p>
   </body>
</html>
Output:
pressed key code output

event.keyCode- returns the pressed key code in the textbox.

here input a letter 'A' is pressed in the text field and returns a 'Key code is 65'.

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^