English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML Reference Manual

Complete List of HTML Tags

HTML onkeydown Event Attribute

The onkeydown attribute is used to get or set the event handler function for the keydown event of the current element

HTML Event Attributes

Online Example

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 ‹/›

Browser Compatibility

IEFirefoxOperaChromeSafari

All major browsers support the onkeydown event attribute

Definition and Usage

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>.

HTML 4.01 and HTML5Differences between

No difference.

Syntax

<element onkeydown="script">

Attribute Value

ValueDescription
scriptSpecifies the script to be executed when the onkeydown event is triggered.


HTML Event Attributes