jQuery keypress event
The keypress event is triggered after a key is pressed in keyboard. The keypress() method will be used to perform any action or run a function as keypress event occurs in your web document.
Keypress event occurs when a key is pressed down, so it is quite similar to keydown event. However there are differences between the two – for example pressing a few keys will trigger only keydown event like ALT, CTRL, ESC etc.
Also keypress event returns different values for lowercase and uppercase letters, whereas keyup and keydown return same. So its useful to use keypress event in case you have to differntiate between lowercase and uppercase.
This can be useful for many scenrios, for example in passwords boxes only allowing letters, numbers and a few special characters while notifying users if certain key is not allowed. In that case rather sending request to server and then checking input by visitor one can check it at client side.
Syntax of keypress event
Where function is optional, to execute as keypress event occurs.
Running example of keypress event
The example below shows when a key (in your keyboard) is pressed, and keypress event occurs, keypress() method will capture the event and throw an alert with ASCII value of the pressed key.
alert("keypress event occured! The ASCII value of pressed key is: " + event.keyCode);
Enter keyboard keys: <input type="text" id="keyboard">