Keyboard
Keyboard Event
HTML keyboard
event attributes are used to handle user interactions involving keyboard input, such as key presses and releases. These events help manage actions like form submissions or triggering functions based on keyboard inputs.
Keyboard Events
Event | Occurs When |
---|---|
onkeydown | Triggered when a key is pressed down. |
onkeypress | Triggered when a key is pressed and held. |
onkeyup | Triggered when a key is released. |
KeyboardEvent Properties
Property | Returns |
---|---|
altKey | Indicates if the ALT key was pressed. |
charCode | Deprecated (Avoid using it). |
code | The code of the key that triggered the event. |
ctrlKey | Indicates if the CTRL key was pressed. |
isComposing | Indicates if the event is in a composing state. |
key | The value of the key that triggered the event. |
keyCode | Deprecated (Avoid using it). |
location | The location of the key on the keyboard/device. |
metaKey | Indicates if the META key was pressed. |
repeat | Indicates if a key is being held down repeatedly. |
shiftKey | Indicates if the SHIFT key was pressed. |
which | Deprecated (Avoid using it). |
KeyboardEvent Methods
Method | Description |
---|---|
getModifierState() | Returns true if the specified modifier key (like SHIFT, CTRL) is active. |
Conclusion
HTML keyboard events like onkeydown
, onkeyup
, and onkeypress
enable dynamic responses to user key interactions. Properties like altKey
, ctrlKey
, and shiftKey
provide additional context about the key state. These events help create interactive, keyboard-driven web experiences.
onsubmit
The onsubmit event in HTML is triggered when a <form> is submitted, allowing scripts to validate the data or perform actions before the <form> submission is completed.
onkeydown
The onkeydown event in HTML is triggered when a key is pressed, enabling developers to capture keyboard input for functions such as shortcuts or commands.