Keyboard

HTML keyboard event attributes manage user interactions involving key presses and releases, enabling dynamic responses to keyboard input.

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

EventOccurs When
onkeydownTriggered when a key is pressed down.
onkeypressTriggered when a key is pressed and held.
onkeyupTriggered when a key is released.

KeyboardEvent Properties

PropertyReturns
altKeyIndicates if the ALT key was pressed.
charCodeDeprecated (Avoid using it).
codeThe code of the key that triggered the event.
ctrlKeyIndicates if the CTRL key was pressed.
isComposingIndicates if the event is in a composing state.
keyThe value of the key that triggered the event.
keyCodeDeprecated (Avoid using it).
locationThe location of the key on the keyboard/device.
metaKeyIndicates if the META key was pressed.
repeatIndicates if a key is being held down repeatedly.
shiftKeyIndicates if the SHIFT key was pressed.
whichDeprecated (Avoid using it).

KeyboardEvent Methods

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