Home >Web Front-end >HTML Tutorial >html event attribute onkeydown triggered when the user presses a key
Example
Execute a section of JavaScript when the user presses a key:
<input type="text" onkeydown="displayResult()">
Browser support
IE
Firefox
Chrome
Safari
Opera
All major browsers support onkeydown Attributes.
Definition and Usage
The onkeydown property is triggered when the user presses a key (on the keyboard).
Tips: Event order relative to onkeydown event :
onkeydown onkeypress onkeyup
Comments :onkeydown attribute does not apply to the following elements: dde6fb694e6711ae5e6f381704c04ae4, 71af07a0e88a1ac1ff73f855702ac153, 0c6dc11e160d3b678d68754cc175188a, 93f0f5c25f18dab9d176bd4f6de5d30e, 100db36a723c770d327fc0aef2ce13b1, d5ba1642137c3f32f4f4493ae923989c, e8e496c15ba93d81f6ea4fe5f55a2244, 0c68fef83818661b6da588c77ca3985e, 27835793f4768f4164d1421d99e293bc, c9ccee2e6ea535a969eb3f532ad9fe89 or b2386ffb911b14667cb8f0f91ea547a7.
Differences between HTML 4.01 and HTML5
None.
Syntax
<element onkeydown="script">
Attribute value
Value | |
script | Script to run when onkeydown occurs. |
onkeydown Gets the keys pressed by the user
The following is an example of using the onkeydown event to obtain the information about the keyboard keys pressed by the user:
<html> <body> <script type="text/javascript"> function noNumbers(e) { var keynum; var keychar; keynum = window.event ? e.keyCode : e.which; keychar = String.fromCharCode(keynum); alert(keynum+':'+keychar); } </script> <input type="text" onkeydown="return noNumbers(event)" /> </body> </html>
The above is the detailed content of html event attribute onkeydown triggered when the user presses a key. For more information, please follow other related articles on the PHP Chinese website!