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 ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.