Last Updated : 11 Jul, 2025
This attribute fires when a user presses a key on the Keyboard. This event attribute can not be used for all keys (e.g. ALT, CTRL, SHIFT, ESC) in all browsers.
Supported Tags: All HTML elements, EXCEPT:
Syntax:
<element onkeypress="script">
Attribute: The onkeydown attribute works for all keys in all browsers. The script will be run when onkeypress attribute call.
Note: This attribute has been DEPRECATED and is no longer recommended. The order of events that is related to the onkeypress events:
Example:
HTML
<!DOCTYPE html>
<html>
<head>
<title>onkeypress attribute</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h2>onkeypress attribute</h2>
<input type="text" onkeypress="GeeksForGeeks()">
<script>
function GeeksForGeeks() {
alert("Welcome to GeeksForGeeks!");
}
</script>
</body>
</html>
Output:
HTML onkeypress Attribute Example OutputSupported Browsers: The browser supported by onkeypress event attribute are listed below:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4