Here is another simple jQuery snippet which tells which key is pressed.
It’s so simple to find out which key is pressed using jQuery event. See the below snippet
JS Snippet:
1 2 3 4 5 6 |
$(function(){ $(document).keydown(function(e){ console.log(e.key); console.log(e.keyCode); }); }); |
A full demo script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script> $(function(){ $(document).keydown(function(e){ $('#keyname').text(e.key); $('#keycode').text(e.keyCode); }); }); </script> </head> <body> <div class="container"> <div class="row"> <h2>jQuery to find which key is pressed!</h2> <hr /> <div class="form-group"> <label for="start">Start Typing:</label> <table class="table table-striped table-bordered"> <tr><th>Key Name</th><th>Key Code</th></tr> <tr><td id="keyname">None</td><td id="keycode">None</td></tr> </table> </div> </div> </div> </body> </html> |
You can see the demo of the above script here: