The keypress() method in jQuery triggers the keypress event whenever browser registers a keyboard input. So, Using keypress() method it can be detected if any key is pressed or not.
To trigger the keypress event for the selected elements:
To attach a function to the keypress event:
Below code is used to check if a key is pressed anywhere on the page or not.
To detect any specific key event.keyCode and event.which will return an ASCII value of the pressed key, Both are browser specific.Below code is used to check which key is pressed on the page.
If any key is pressed then output will be-
Below code is used to check if Enter key is pressed inside a textbox on the page or not.
When enter key is pressed inside the above textbox then output becomes like below-
Note: The keypress event is not triggered for all keys like non printing characters (e.g. ALT, CTRL, SHIFT, ESC). keydown() method can be used to check these keys.
This article is attributed to GeeksforGeeks.org