Yogesh Chauhan's Blog

How to catch .keypress() on body using jQuery?

in jQuery on January 14, 2021

Example

Explanation

keypress() binds an event handler to the “keypress” event. It can also trigger that event on any element.

When the browser records keyboard input, the keypress event is sent to an element.

It is similar to the keydown event, except that some keys like modifier and non-printing keys (Shift, Esc, delete) trigger keydown events but they cannot trigger keypress events.

We can attach keypress event handler to any element, but the event is only sent to the element with focus.

All browsers use different properties to store this information. So, jQuery normalizes the .which property so we can reliably use it to retrieve the character code.

While keydown and keyup provide a code that indicates which key is pressed, the keypress provides info on which character was entered.

For example, keydown and keyup indicates 65 when we press a lowercase “a”, but keypress will indicate it as 97.

Interestingly, all events reports 65 for an uppercase “A”.

That’s why .keydown() or .keyup() is a better choice when catching special keystrokes such as arrow keys, .

Learn more about it on Official Docs.


amazon

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS? #Aug 1 What is Zdog? #Aug 1 How to add before after image effect using pure CSS? #Jul 31 Add animation to your skills bar using CSS #Jul 31 Use SwiperJS to create mobile touch sliders fast
You might also like these
How to set default timezone using PHP?PHPThe substr() method in JavaScript and how it’s different from substring()JavaScriptA Quick Comparison of JOIN and Subquery in SQLSQL/MySQLinclude, include_once, require, require_once in PHPPHPHow to uninstall Cocoapods from the Mac OS?MiscellaneousHow to implement NSNumberFormatter in Swift for currency?SwiftCSS Attribute SelectorsCSSHow to animate list items using CSS and JavaScript?CSSHow to use PHPMailer to send an Email via Gmail SMTP Server?PHPCreate a responsive pricing table using simple HTML and CSSCSSCheck if mixin is being called in a style rule or not in SCSSSCSSFull and Partial CUBE in Postgres with ExamplesPostgres