jQuery event.which and Modern Key Detection

jQuery 4 note: jQuery no longer provides its old event.which normalization shim. Older examples that depend on a normalized numeric key code should be updated.

This URL is retained because event.which was widely used in older jQuery keyboard tutorials. For current code, use event.key for the key value and event.code when the physical key location matters.

Older event.which Pattern Top ↑

$( document ).on( "keydown", function (event) {
    // Legacy code often used:
    $( "#t1" ).text( "Key number: " + event.which );
});

Open the legacy event.which demo →

Modern Key Identification Top ↑

$( document ).on( "keydown", function (event) {
    $( "#t1" ).text(
        "Key: " + event.key + " | Code: " + event.code
    );
});

event.key vs event.code Top ↑

  • event.key: the meaning/value of the key, such as "a", "A", "Enter" or "Escape".
  • event.code: the physical keyboard position, such as "KeyA" or "ArrowLeft".

For shortcut logic, choose the property that matches the intent of the interface rather than converting everything to a numeric code.

Example: Detect Enter Top ↑

$( "#name" ).on( "keydown", function (event) {
    if ( event.key === "Enter" ) {
        event.preventDefault();
        $( "#result" ).text( "Enter pressed" );
    }
});

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