O'Reilly logo

jQuery and JavaScript Phrasebook by Brad Dayley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Determining What Key Was Pressed

$("input[type=text]").on("keypress",   function(e){     var s = "You Pressed ";     if (e.ctrlKey){ s += "CTRL + "; };     if (e.altKey){ s += "SHIFT + "; };     if (e.shiftKey){ s += "SHIFT + "; };     s += String.fromCharCode(e.charCode);     $("#p1").html(s);     }); });

The event passed to the event keyboard event handler includes the character code and the auxiliary key information. To determine which key was pressed, use the event.charCode attribute to get the character code. You can convert the character code to a character using the String.fromCharCode(code) method. For example:

var charcter = String.fromCharCode(event.charCode);

To determine ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required