English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The onkeydown attribute is used to get or set the event handler function for the keydown event of the current element
Execute JavaScript when the user presses a key
<!DOCTYPE html> <html> <head> <title>Usage of HTML onkeydown Event Attribute (Basic Tutorial Website oldtoolbag.com)</title> <script> function demo_onkeydown() { var x; if(window.event) // IE8 and earlier IE versions { x=event.keyCode; } else if(event.which) // IE9/Firefox/Chrome/Opera/Safari { x=event.which; } var keychar=String.fromCharCode(x); alert("The key is pressed ") + keychar + "The key is pressed"); } </script> </head> <body> <p>When the user presses a key in the input field, the function is triggered. It reminds the pressed key.</p> <input type="text" onkeydown="demo_onkeydown()"> </body> </html>Test See ‹/›
IEFirefoxOperaChromeSafari
All major browsers support the onkeydown event attribute
The onkeydown attribute is triggered when the user presses a key (on the keyboard).
Tip: Event trigger sequence related to onkeydown event:
onkeydown
onkeypress
onkeyup
Note: The onkeydown attribute cannot be used with the following elements: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, or <title>.
No difference.
<element onkeydown="script">
Value | Description |
---|---|
script | Specifies the script to be executed when the onkeydown event is triggered. |