YouTip LogoYouTip

Ev Onkeydown

# HTML onkeydown Event Attribute The `onkeydown` event attribute triggers a script when a user presses a key on the keyboard. It is a fundamental keyboard event used to capture user input, build interactive shortcuts, and control web applications. --- ## Introduction The `onkeydown` attribute is part of the HTML Event Attributes set. It fires the moment a key on the keyboard is pressed down, even before the character is rendered on the screen. ### The Keyboard Event Sequence When a user presses and releases a key, a sequence of three events occurs in order: 1. **`onkeydown`** (The key is pressed down) 2. **`onkeypress`** (The key is pressed and produces a character value - *Deprecated in modern web standards*) 3. **`onkeyup`** (The key is released) --- ## Browser Support | Chrome | Edge | Firefox | Safari | Opera | | :---: | :---: | :---: | :---: | :---: | | Yes | Yes | Yes | Yes | Yes | The `onkeydown` attribute is fully supported by all major modern web browsers. --- ## Syntax You can define the `onkeydown` attribute inline within an HTML element: ```html ``` ### Attribute Values | Value | Description | | :--- | :--- | | *script* | The JavaScript code or function to be executed when the `onkeydown` event is triggered. | --- ## Code Examples ### Example 1: Basic Inline Usage This example triggers a JavaScript function when a user presses a key inside the text input field. ```html onkeydown Demo

Press any key inside the input box below:

``` ### Example 2: Detecting Which Key Was Pressed In modern web development, it is common to pass the event object (`event`) to determine exactly which key was pressed using the `event.key` or `event.code` properties. ```html Detect Key Press

Type something in the input field to see the key name:

``` --- ## Considerations & Best Practices ### Unsupported Elements The `onkeydown` attribute cannot be used with the following HTML elements: * ``, ``, `
`, ``, ``, `