jQuery Keypress Event

Beginner
7 min read
Updated: Oct 2026
4 examples
Live preview

What Is keypress?

The keypress event fires when the browser registers printable character input. Unlike keydown, modifiers and non-printing keys (Shift, Esc, Delete) usually do not trigger it. Bind with .on("keypress") (since jQuery 1.7); the event reaches only the focused element.

Remember
$("#target").on("keypress", handler)   // bind (1.7+)
$("#target").trigger("keypress")       // fire handlers (1.0+)

event.which → character code
  "a" → 97    "A" → 65    Enter → 13

arrows / Shift / Esc → usually NO keypress
(use keydown instead)
NeedUse
Printable characters.on("keypress", fn)
All keys / arrows.on("keydown", fn)
Key release.on("keyup", fn)
Modern text fieldsinput event (prefer for new UI)

How to Use keypress

Focus a field, bind a handler, then read event.which when a character is typed.

  1. Pick a focusable target — usually an <input> or <textarea>.
  2. Bind with $("#target").on("keypress", handler) (jQuery 1.7+).
  3. Inside the handler, read event.which for the character code.
  4. Call event.preventDefault() when you need to block a character (for example Enter).
PieceRequired?Role
Focused elementYesOnly the focused element receives keypress
handlerYesfunction(event) { … }
eventDataOptionalObject passed as event.data
Return—jQuery object (chainable)

Pseudocode

Pseudocode
bind keypress on focused element

when user types a printable character:
  code = event.which          // character code
  if code is Enter (13):
    preventDefault()          // optional block
  else:
    use code / fromCharCode(code)

when user presses arrow / Shift / Esc:
  keypress usually does NOT fire
  → use keydown instead

Cheat Sheet

Keep these facts handy while handling typed characters.

TopicDetail
Bind$("#el").on("keypress", fn)
Trigger$("#el").trigger("keypress")
Unbind$("#el").off("keypress")
Character codeevent.which — a=97, A=65
Typed charString.fromCharCode(event.which)
vs keydownkeydown = key; keypress = character
Shorthand.keypress() — deprecated; prefer .on

Live Preview

Focus the field and type a printable character. See event.which update live — arrows will not fire keypress.

Browser demo of jQuery keypress — same idea as the examples below.

Result
$("#field").on("keypress", …)
→ type a printable key to see event.which

/* arrows / Shift do not fire keypress */

jQuery Examples

Short, copy-ready snippets from the official API style. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Bind a Keypress Handler

Log when a printable key is pressed while the input has focus.

jQuery
$("#target").on("keypress", function () {
  console.log("Handler for keypress called.");
});
Try It Yourself

How It Works

  1. Select the input. $("#target") must be able to receive focus.
  2. Bind keypress. .on("keypress", fn) registers the handler (jQuery 1.7+).
  3. Type a letter. Printable input fires the handler; the message logs to the console.
  4. Non-printing keys skip. Arrows and Shift do not call this handler — use keydown for those.

Example 2 — Trigger Keypress from a Button

Fire the bound handler programmatically with .trigger("keypress").

jQuery
$("#target").on("keypress", function () {
  console.log("Handler for keypress called.");
});

$("#other").on("click", function () {
  $("#target").trigger("keypress");
});
Try It Yourself

How It Works

  1. Bind first. Same handler as Example 1 on #target.
  2. Click the button. The click handler runs $("#target").trigger("keypress").
  3. Handlers fire once. Bound keypress listeners run; this is not a full OS key simulation.
  4. Useful for demos/tests. Pass a fake event object if you need a specific which.

Example 3 — Character Code and Block Enter

Count presses, log event.which, and stop Enter with preventDefault.

jQuery
var count = 0;

$("#target").on("keypress", function (event) {
  if (event.which === 13) {
    event.preventDefault();
  }
  count++;
  $("#log").text(
    "Called " + count + " time(s). Char: " + event.which
  );
});
Try It Yourself

How It Works

  1. Read event.which. On keypress this is the character code.
  2. Block Enter. Code 13 → preventDefault() stops the default action.
  3. Count each fire. Every printable key increments count and updates the log.
  4. Official pattern. Matches the jQuery API demo idea: which + Enter guard.

Example 4 — Keypress vs Keydown Codes

Bind both events on one input to see how event.which differs.

jQuery
$("#target")
  .on("keypress", function (event) {
    $("#press").text("keypress which: " + event.which);
  })
  .on("keydown", function (event) {
    $("#down").text("keydown which: " + event.which);
  });
Try It Yourself

How It Works

  1. Two listeners. Same input listens for both keypress and keydown.
  2. Character vs key. Lowercase a → 97 on keypress, 65 on keydown.
  3. Uppercase matches. A reports 65 on both events.
  4. Arrows prove the gap. Only keydown updates — keypress never fires.

Edge Cases & Pitfalls

Check these before relying on keypress in real projects.

focus

Needs focus

keypress is delivered only to the focused element. Click the field first, or bind on document for page-wide shortcuts (still prefer keydown for those).

arrows

No arrows / modifiers

Shift, Esc, Delete, and arrow keys typically skip keypress. Use keydown.

codes

Do not mix code meanings

On keypress, which is a character code. On keydown/keyup it is a key code. Lowercase a is 97 vs 65 — easy to confuse.

paste

Filtering misses paste

Blocking characters on keypress alone does not stop paste or autofill. Validate the final value too (or use the input event).

Browser Support

jQuery’s .on("keypress") (since 1.7) and .trigger("keypress") (since 1.0) work wherever jQuery runs. Native keypress is non-standard — prefer input (text) or keydown (all keys) in new projects.

✓ Non-standard · jQuery 1.7+

jQuery keypress event

Works in Chrome, Firefox, Safari, and Edge with jQuery loaded. Behavior can vary by browser/platform because keypress is not in an official DOM spec. Prefer .on('keypress') over deprecated .keypress().

Legacy Legacy / varies
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
keypress Legacy event

Bottom line: Fine for reading legacy jQuery. For new UI, prefer the input event for text or keydown for all keys. Always bind with .on('keypress'), not deprecated .keypress().

Performance Notes

ApproachCostBest for
.on("keypress", fn)Per characterLightweight character filters
.on("keydown", fn)Per key (+ repeat)Shortcuts; held keys repeat
input eventPer value changeModern text validation (incl. paste)

Handlers are cheap if they stay small. Avoid heavy DOM work on every keystroke; debounce live search. Prefer input when you care about the field value, not each physical key.

Key Takeaways

  • Role: react to printable character input on the focused element.
  • API: bind with .on("keypress"); fire with .trigger("keypress").
  • Codes: event.which is the character code (a=97, not 65).
  • Limits: non-standard; prefer keydown or input for new work.

One line: keypress is for typed characters — use event.which for the character code, and switch to keydown when you need every key.

Frequently Asked Questions

keypress fires when the browser registers keyboard input that produces a character. Modifier and non-printing keys such as Shift, Escape, and Delete trigger keydown but usually not keypress. Bind with .on('keypress', handler) since jQuery 1.7. The event reaches only the focused element.
keydown fires for every key, including arrows and modifiers. keypress is aimed at printable character input. keydown reports the key (lowercase a = 65); keypress reports the character (lowercase a = 97). For arrows and shortcuts, prefer keydown.
Use event.which — jQuery normalizes it across browsers. On keypress it is the character code: lowercase a is 97, uppercase A is 65, Enter is 13. String.fromCharCode(event.which) gives the typed character when appropriate.
keypress is not covered by any official DOM specification, so behavior can vary. Many modern apps prefer the input event for text fields or keydown for all keys. Keep keypress for legacy jQuery code, and know its limits.
Call $('#target').trigger('keypress') since jQuery 1.0. Bound handlers run. .trigger() does not fully simulate a real typed character unless you pass a simulated event with the correct which value.
Arrows, Shift, Ctrl, Escape, and other non-printing keys do not produce character input, so browsers typically skip keypress. Use .on('keydown') and check event.which (37–40 for arrows) when you need those keys.

Next: .keypress() method

Learn the deprecated shorthand and how to migrate to .on("keypress").

.keypress() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful