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.
- Trigger a keypress/keydown/keyup event in JS/jQuery
- AngularJS | ng-keypress Directive
- jQuery | jQuery.support Property
- jQuery | jQuery.fx.interval Property with example
- jQuery | jquery Property
- jQuery | jQuery.fx.off Property
- jQuery | one() with Examples
- jQuery | is() Method
- jQuery | contains() Method
- jQuery | #id Selector
- Get the current URL using jQuery?
- jQuery | offsetParent() with Example
- jQuery | val() with Examples
- JQuery | map() Method
- How to get the value of a textarea in jQuery ?
- jQuery | Dimensions
- jQuery | parentsUntil() with Example
- jQuery | Traversing
- How to use .on and .hover in jQuery ?
- jQuery | has() with Examples
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.