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.
binding event key keypress method