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.
- AngularJS | ng-keypress Directive
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | jQuery.fx.off Property
- jQuery | one() with Examples
- jQuery | parentsUntil() with Example
- jQuery | :even Selector
- jQuery | get() Method
- jQuery | on() with Examples
- jQuery | last() with Examples
- jQuery | :gt() Selector
- How to use jQuery in Angular ?
- jQuery | Dimensions
- jQuery | Traversing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.