onkeypressEvent

事件对象参考手册Event Object

Example

Execute JavaScript code when the user presses a keyboard key:

<input type="text" onkeypress="myFunction()">

Try it yourself »

Definition and Usage

The onkeypress event occurs when a keyboard key is pressed and released.

Tip:The execution order of events associated with the onkeypress event:

  1. onkeydown
  2. onkeypress
  3. onkeyup

Note:In all browsers, the onkeypress event can only listen for letters and numbers, and cannot listen for some special keys (ALT, CTRL, SHIFT, ESC, arrow keys, etc.). To listen for whether a user has pressed a key, use theonkeydownevent, all browsers support the onkeydown event.

Syntax

In HTML:

<element onkeypress="SomeJavaScriptCode">

In JavaScript:

object.onkeypress=function(){SomeJavaScriptCode};

Parameter Description
SomeJavaScriptCode Required. Specifies the JavaScript to execute when the event occurs.


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the onkeypress event.


HTML tags that support the onkeypress event:

The onkeypress attribute can apply to all HTML elements, except: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title>.


事件对象参考手册Event Object Other Extensions