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.fx.interval Property with example
- jQuery | jQuery.support Property
- jQuery | jquery Property
- jQuery | jQuery.fx.off Property
- jQuery | innerWidth() with Example
- jQuery | Syntax
- jQuery | finish() with Example
- jQuery | is() Method
- jQuery | :last Selector
- How to use .on and .hover in jQuery ?
- jQuery | parentsUntil() with Example
- jQuery | contains() Method
- jQuery | :first Selector
- jQuery | #id Selector
- jQuery | has() with Examples
- jQuery | die() Method
- JQuery | now() method
- jQuery UI | Tooltip
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.