Determine Which Key Was Pressed Using jQuery

You can easily run into situations where you might want to check which key was pressed by the user. For Example, I was working on the project where I wanted to Check for Each and Every Alphabet Entered by the User in the form. Since, I am Huge Fan of jQuery I found a jQ method and applied it successfully.

Here is the Piece of Code which will help you determine the key pressed by the user.
{code type=javascript}
var key = String.fromCharCode(event.keyCode){/code}

Just Using this piece of code will not help. This Code should be a part of a keypress() function. And Here is a full Implementation of how it would work.

{code type=javascript}
$(document).keypress( function(event) {
var key = String.fromCharCode(event.keyCode).toUpperCase();
alert(key); //This will alert the key u pressed.
This Keypress Function is applied to the full Website, i.e. if the key is pressed any where in your site, the function will be executed. If you want to apply it to a particular form, then simply replace the $(document) by $(‘#idOfTheForm’).

I Have Also Converted the key to an uppercase character, because that’s what I wanted. If you want the exact case or lower case character, you may apply a corresponding function or no function at all.

One thought on “Determine Which Key Was Pressed Using jQuery

  1. Keypress Function is Not Working in Firefox ? Here’s An Alternate. |

Leave a Reply

Your email address will not be published. Required fields are marked *