onkeydown Event
Example
Call a function when the user presses a key:
<input type="text" onkeydown="myFunction()">
Try it Yourself »
More examples below.
Description
The onkeydown
event occurs when the user presses a key on the keyboard.
Keyboard Events
Event | Occurs When |
---|---|
onkeydown | The user presses a key |
onkeypress | The user presses a key |
onkeyup | the user releases a key |
See Also:
Warning
The onkeypress event is deprecated.
It is not fired for all keys (like ALT, CTRL, SHIFT, ESC) in all browsers.
To detect if the user presses a key, always use the onkeydown event. It works for all keys.
Syntax
In JavaScript, using the addEventListener() method:
object.addEventListener("keydown", myScript);
Try it Yourself »
Technical Details
Bubbles: | Yes |
---|---|
Cancelable: | Yes |
Event type: | KeyboardEvent |
HTML tags: | All HTML elements, EXCEPT: <base>, <bdo>, <br>, <head>, <html>, <iframe>, <meta>, <param>, <script>, <style>, and <title> |
DOM Version: | Level 2 Events |
More Examples
Example
Using "onkeydown" together with the "onkeyup" event:
<input type="text" onkeydown="keydownFunction()" onkeyup="keyupFunction()">
Try it Yourself »
Browser Support
onkeydown
is a DOM Level 2 (2001) feature.
It is fully supported in all browsers:
Chrome | Edge | Firefox | Safari | Opera | IE |
Yes | Yes | Yes | Yes | Yes | 9-11 |