HTML onkeypress Event Attribute

Description

The event attribute onkeypress allows you to set the script to fire after the user presses a key. The onkeypress event does not fire for all keys (for example, Alt, Ctrl, Shift, Esc, PrScr, etc). To detect when a user presses any key, use the onkeydown event attribute.

Syntax

<element onkeypress="script">

Values

  • scriptThe name of the script to use when the event has been triggered.

Example

<!DOCTYPE html>
  <html>
    <head>
      <title>Event Attribute onkeypress</title>
    </head>
  <body>
    <p>Click any button in the form to run the script:</p>
    <input type="text" onkeypress="keyboardTest()">
<script>
function keyboardTest() {
  alert("You see this window because you triggered the script"); 
}
</script>
  </body>
</html>

Supported Tags

All HTML elements except:
<base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style> and <title>

Browser Support

Desktop
Explorer Edge Chrome Firefox Opera Safari
XYesYesYesXX
Tablets / Mobile
Android Chrome Firefox Opera Safari Samsung
YesYesYesXXX