jQuery keypress Event (Legacy)

Legacy event: keypress is kept here because older jQuery code and searches still refer to it. For new code, prefer keydown for keyboard commands and the input event for text-entry changes.

The historical keypress event was associated mainly with character-producing keys and was not dependable for keys such as arrows, Shift or Escape.

Legacy Syntax Top ↑

$( selector ).on( "keypress", function (event) {
    // Legacy character-key event.
});

Legacy keypress Example Top ↑

$( function () {
    $( document ).on( "keypress", function (event) {
        alert( "keypress: " + event.key );
    });
});

keypress vs keydown Top ↑

The important practical difference is that keydown is the reliable choice for general keyboard handling, including non-character keys. keypress should not be the basis of new keyboard interfaces.

$( document ).on( "keydown", function (event) {
    $( "#d1" ).text( "keydown: " + event.key );
});

$( document ).on( "keypress", function (event) {
    $( "#d2" ).text( "keypress: " + event.key );
});
<div id="d1"></div>
<div id="d2"></div>

Open the comparison tutorial →

Modern Replacement Top ↑

$( document ).on( "keydown", function (event) {
    if ( event.key === "Escape" ) {
        $( "#message" ).text( "Escape pressed" );
    }
});

For text fields, listen to input when your goal is to react to the resulting text rather than a particular physical key.

keydown() · keyup() · Key-hold duration

Legacy key-number demo

Video Tutorial on Keyboard Events






plus2net.com






✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer