Skip to main content
Oct 12, 2013

In this tutorial we're providing example for determining which keyboard's key is pressed. Here is a part of HTML code we'll use for the example:

<input type="text" id="testbox" />

The following jQuery code gets the event and shows the pressed key as a numerical code:

$(document).ready(function() {
    $('#testbox').keypress(function(event){
        $('p').text('Pressed key is: '+event.keyCode);
    });
});

We can also display pressed key as a character using jQuery code from below:

$(document).ready(function() {
    $('#testbox').keypress(function(event){
        $('p').text('Pressed key is: '+String.fromCharCode(event.keyCode));
    });
});

Using keypress() function without parameters serves to trigger the event manually.