The keypress event fires when the browser registers printable keyboard input — the character the user is typing. This tutorial covers .on("keypress"), event.which character codes, eventData, .trigger("keypress"), preventDefault for Enter, keypress vs keydown, and the official jQuery API demos.
01
.on()
Bind handler
02
event.which
Char code
03
.trigger()
Fire keypress
04
Focus
Target element
05
char
Printable
06
Since 1.7
Modern API
Fundamentals
Introduction
When you need to react to typed characters — not arrow keys or Shift — the keypress event is a classic hook. It fires when the browser registers keyboard input that produces a character, similar to keydown but narrower in scope.
The official jQuery API notes that keypress is not covered by any official specification — actual behavior may differ across browsers, versions, and platforms. Bind the modern event with .on("keypress") since 1.7 and trigger with .trigger("keypress") since jQuery 1.0.
Concept
Understanding the keypress Event
The keypress event is sent to an element when the browser registers keyboard input. It is similar to keydown, except that modifier and non-printing keys such as Shift, Esc, and Delete trigger keydown but not keypress.
It can be attached to any element, but the event is only delivered to the element that has focus. Form controls — <input> and <textarea> — are always reasonable targets.
💡
Character vs key code
keydown and keyup report which key was pressed. keypress reports which character was entered. Lowercase “a” is 65 in keydown/keyup but 97 in keypress. Uppercase “A” is 65 in all three. For arrow keys, use .on("keydown").
Foundation
📝 Syntax
The modern jQuery API for the keypress event has two main forms — binding a handler and triggering the event:
.on() and .trigger() return the original jQuery object for chaining.
Returning false from the handler can cancel default actions — same as preventDefault() + stopPropagation().
Cheat Sheet
⚡ Quick Reference
Goal
Code
Bind keypress handler
$("#target").on("keypress", fn)
Read character code
event.which — 97 lowercase a, 65 uppercase A
Pass data to handler
$("#field").on("keypress", { max: 10 }, fn)
Block Enter in input
if (event.which === 13) event.preventDefault()
Get typed character
String.fromCharCode(event.which)
Trigger keypress programmatically
$("#target").trigger("keypress")
Remove keypress handlers
$("#target").off("keypress")
Compare
📋 keypress vs keydown vs keyup vs deprecated .keypress()
Four related keyboard concepts — keypress is for characters; keydown is for all keys.
keypress event
char
Printable character input — event.which is character code; no arrow keys
keydown event
key down
Every key including arrows and modifiers — use for shortcuts and games
keyup event
key up
Fires when key is released — pair with keydown for hold detection
.keypress() method
deprecated
Old binding shorthand — use .on("keypress") and .trigger("keypress") instead
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Examples 3–5 extend the official counter demo with character-code comparison and a digits-only filter. Use the Try-it links to run each snippet in the browser.
📚 Binding & Triggering
Official jQuery demos for keypress handlers and programmatic triggers.
Example 1 — Official Demo: Bind Handler on #target
Log to the console when a printable key is pressed while the input has focus.
Focus #target, type a letter or number → console logs handler message
Arrow keys and Shift do NOT fire keypress (use keydown instead)
Open DevTools Console to see output
How It Works
.on("keypress", fn) runs when the browser registers printable input while #target has focus. Non-printing keys are ignored — that is the main difference from keydown.
Example 2 — Official Demo: #other Triggers Keypress on #target
One button programmatically fires the keypress handler on the input.
Click Trigger the handler → trigger("keypress") → console log runs
Same handler as typing a printable character in #target
Useful for testing keypress logic in unit demos
How It Works
.trigger("keypress") synthetically fires the event. Bound handlers run; pass a simulated event if you need a specific event.which value.
📈 Character Codes & Filtering
Official extended demo plus practical patterns for character codes and digit filters.
Example 3 — Official Demo: Counter, event.which, and Enter preventDefault
Count keypress events, block Enter, and log the event object — from the jQuery API documentation.
jQuery
var xTriggered = 0;
$( "#target" ).on( "keypress", function( event ) {
if ( event.which == 13 ) {
event.preventDefault();
}
xTriggered++;
var msg = "Handler for `keypress` called " + xTriggered + " time(s).";
$( "#log" ).prepend( "
Each printable key increments counter and logs event.which (character code)
Type "a" → Char: 97 | Type "A" → Char: 65
Enter (13) → preventDefault | Trigger button also increments
How It Works
On keypress, event.which holds the character code. Compare to keydown where lowercase “a” reports 65 instead of 97.
Example 4 — Compare Character Code: Keypress vs Keydown
Bind both events on the same input to see how event.which differs for the same keystroke.
Type lowercase "a" → keypress: 97, keydown: 65
Type uppercase "A" → both report 65
Press arrow key → keydown updates, keypress does not fire
How It Works
The official API uses this distinction: keydown/keyup report the key; keypress reports the character. Choose the event that matches what you need to detect.
Example 5 — Allow Digits Only with eventData
Block non-digit characters on keypress — a common numeric field pattern.
Type "5" → allowed (char code 53)
Type "a" → preventDefault, warning shown
event.data.min/max from eventData — codes 48-57 are 0-9
How It Works
Character codes 48–57 are digits 0–9. preventDefault() on keypress stops the character from appearing. For production numeric fields, also validate pasted input.
Applications
🚀 Common Use Cases
Numeric-only fields — filter digits on keypress with event.which between 48 and 57.
Live character preview — show the typed character with String.fromCharCode(event.which).
Enter to submit vs block — preventDefault on Enter (char code 13) in single-line inputs.
Legacy form validation — older tutorials bind keypress to restrict allowed characters.
Learning character codes — compare keypress vs keydown side by side while debugging.
Reading old codebases — recognize .on("keypress") before migrating to input or keydown.
🚀 How the keypress Event Flows
1
User types character
Printable key produces input while a focused element is active — or code calls .trigger("keypress").
type
2
keypress event fires
Handler runs on the focused element. event.which holds the character code — not the physical key code for lowercase letters.
keypress
3
Handler decides
Filter digits, block Enter with preventDefault(), or update UI from event.which.
filter?
4
⌨
Character appears or blocked
Unless prevented, the character is inserted. Non-printing keys never reach this step — they only fire keydown/keyup.
Important
📝 Notes
Bind with .on("keypress", handler) since jQuery 1.7 — not the deprecated .keypress(handler) method.
Trigger with .trigger("keypress") since jQuery 1.0.
Not in any official DOM spec — behavior may differ across browsers and platforms.
Event is sent only to the element with focus.
event.which on keypress is the character code — lowercase a = 97, not 65.
Arrow keys, Shift, Ctrl, Escape do not fire keypress — use keydown.
For modern text fields, consider the native input event instead of keypress.
Use .off("keypress") to remove handlers.
Compatibility
Browser Support
The keypress event is supported in browsers jQuery targets but is not covered by any official DOM specification — behavior may vary. jQuery’s .on("keypress") (since 1.7) and .trigger("keypress") (since 1.0) normalize event.which for character codes.
✓ jQuery 1.7+
jQuery keypress event
Works in jQuery 1.x, 2.x, and 3.x. Native keypress is deprecated in modern DOM — prefer input event for text or keydown for all keys in new projects. jQuery keeps keypress for legacy compatibility.
VariesLegacy event
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
Bottom line: Safe in legacy jQuery projects. For new code prefer input event (text) or keydown (all keys). Use .on('keypress') not deprecated .keypress() for binding.
Wrap Up
Conclusion
The jQuery keypress event hooks printable character input — when you care about the character typed, not arrow keys or modifiers. Bind handlers with .on("keypress", fn), read event.which for character codes, and fire handlers with .trigger("keypress").
Remember: keypress is legacy and unspecified — behavior varies. For arrow keys and shortcuts, use keydown. For modern text fields, consider the input event. When maintaining old code, understand keypress before migrating.
Use keypress when you only need printable characters
Read event.which as the character code on keypress
Prefer keydown for arrows, Escape, and shortcuts
Prefer input event for modern text-field validation
Bind with .on("keypress", handler) in legacy maintenance
❌ Don’t
Rely on keypress for arrow keys — it will not fire
Assume identical behavior in every browser — keypress is unspecified
Confuse keypress character codes with keydown key codes
Use deprecated .keypress(handler) in new code
Forget to validate pasted input when filtering digits on keypress only
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about the keypress event
Char, filter, compare.
6
Core concepts
.on01
.on("keypress")
Bind
API
which02
event.which
Char code
Detect
⚡03
.trigger()
Fire
Programmatic
focus04
Focus
Required
Target
doc05
printable
Chars only
Shortcut
↩06
legacy
Varies
OS
❓ Frequently Asked Questions
The keypress event fires when the browser registers keyboard input that produces a character. Unlike keydown, modifier and non-printing keys such as Shift, Escape, and Delete trigger keydown but not keypress. Bind handlers with .on('keypress', handler) since jQuery 1.7. The event is only sent to the element that has focus.
keydown fires for every key including arrows, Shift, Ctrl, and function keys. keypress fires mainly for printable character input. keydown reports which key was pressed (lowercase a = 65); keypress reports which character was entered (lowercase a = 97). For arrow keys and shortcuts, use keydown.
Read event.which — jQuery normalizes this across browsers. For keypress, event.which holds the character code: lowercase a is 97, uppercase A is 65, Enter is 13. Use String.fromCharCode(event.which) to get the typed character when appropriate.
The keypress event is not covered by any official DOM specification and behavior can differ across browsers and platforms. Many modern apps use the input event for text fields or keydown for all keys. keypress remains in jQuery for legacy code but know its limitations.
Call $('#target').trigger('keypress') since jQuery 1.0. Bound keypress handlers run. Note that .trigger() does not fully simulate a real typed character unless you pass a simulated event object with the correct which value.
Arrow keys, Shift, Ctrl, Escape, and other non-printing keys do not produce character input, so browsers typically skip keypress for them. Use .on('keydown', handler) and check event.which (37–40 for arrows) when you need those keys.
Did you know?
On keypress, jQuery normalizes event.which to the character code even when browsers exposed charCode or keyCode differently. That is why typing lowercase “a” gives 97 in a keypress handler — while the same key reports 65 on keydown.