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)
Focus a field, bind a handler, then read event.which when a character is typed.
Pick a focusable target — usually an <input> or <textarea>.
Bind with $("#target").on("keypress", handler) (jQuery 1.7+).
Inside the handler, read event.which for the character code.
Call event.preventDefault() when you need to block a character (for example Enter).
Piece
Required?
Role
Focused element
Yes
Only the focused element receives keypress
handler
Yes
function(event) { … }
eventData
Optional
Object 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
Reference
Cheat Sheet
Keep these facts handy while handling typed characters.
Focus #target, type a letter →
Handler for keypress called.
(Arrow keys / Shift do not fire keypress)
How It Works
Select the input.$("#target") must be able to receive focus.
Bind keypress..on("keypress", fn) registers the handler (jQuery 1.7+).
Type a letter. Printable input fires the handler; the message logs to the console.
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");
});
Type lowercase "a" → keypress: 97, keydown: 65
Type uppercase "A" → both: 65
Press arrow → keydown updates; keypress stays
How It Works
Two listeners. Same input listens for both keypress and keydown.
Character vs key. Lowercase a → 97 on keypress, 65 on keydown.
Uppercase matches.A reports 65 on both events.
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).
Compatibility
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().
LegacyLegacy / varies
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
keypressLegacy 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().
Analysis
Performance Notes
Approach
Cost
Best for
.on("keypress", fn)
Per character
Lightweight character filters
.on("keydown", fn)
Per key (+ repeat)
Shortcuts; held keys repeat
input event
Per value change
Modern 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.
Remember
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.