UI Events
Concepts and Usage
The UI Events API defines a system for handling user interactions such as mouse and keyboard input. This includes:
- events that are fired on specific user actions such key presses or mouse clicks. Most of these events fire on the
Elementinterface, but the events relating to loading and unloading resources fire on theWindowinterface. - event interfaces, which are passed into handlers for these events. These interfaces inherit from
Eventand provide extra information specific to the type of user interaction: for example, theKeyboardEventis passed into akeydownevent handler and provides information about the key that was pressed.
In this article
Interfaces
CompositionEventPassed into handlers for composition events. Composition events enable a user to enter characters that might not be available on the physical keyboard.
FocusEventPassed into handlers for focus events, which are associated with elements receiving or losing focus.
InputEventPassed into handlers for input events, which are associated with the user entering some input; for example, using an
<input>element.KeyboardEventPassed into handlers for keyboard up/down events.
MouseEventPassed into event handlers for mouse events, including mouse move, mousing over and out, and mouse button up or down. Note that
auxclick,click, anddblclickevents are passedPointerEventobjects.MouseScrollEventDeprecatedDeprecated, Firefox-only, non-standard interface for scroll events. Use
WheelEventinstead.MutationEventDeprecatedPassed into mutation event handlers, which were designed to allow notifications of changes to the DOM. Now deprecated: use
MutationObserverinstead.UIEventA base from which other UI events inherit, and also the event interface passed into some events such as
loadandunload.WheelEventPassed into the handler for the
wheelevent, which fires when the user rotates a mouse wheel or similar user interface component such as a touchpad.
Events
abortFired when loading a resource has been aborted (for example, because the user canceled it).
auxclickFired when the user clicks the non-primary pointer button.
beforeinputFired just before the DOM is about to be updated with some user input.
blurFired when an element has lost focus.
clickFired when the user clicks the primary pointer button.
compositionendFired when a text composition system (such as a speech-to-text processor) has finished its session; for example, because the user has closed it.
compositionstartFired when the user has started a new session with a text composition system.
compositionupdateFired when a text composition system updates its text with a new character, reflected in an update to the
dataproperty of theCompositionEvent.contextmenuFired just before a context menu is invoked.
dblclickFired when the user double-clicks the primary pointer button.
errorFired when a resource fails to load or can't be processed (for example, if an image is invalid or a script has an error).
focusFired when an element has received focus.
focusinFired when an element is just about to receive focus.
focusoutFired when an element is just about to lose focus.
inputFired just after the DOM has been updated with some user input (for example, some text input).
keydownFired when the user has pressed a key.
keypressDeprecatedFired when the user has pressed a key, only if the key produces a character value. Use
keydowninstead.keyupFired when the user has released a key.
loadFired when the whole page has loaded, including all dependent resources such as stylesheets and images.
mousedownFired when the user presses a button on a mouse or other pointing device, while the pointer is over the element.
mouseenterFired when a mouse or other pointing device is moved inside the boundary of the element or one of its descendants.
mouseleaveFired when a mouse or other pointing device is moved outside the boundary of the element and all of its descendants.
mousemoveFired when a mouse or other pointing device is moved while over an element.
mouseoutFired when a mouse or other pointing device is moved outside the boundary of the element.
mouseoverFired when a mouse or other pointing device is moved over an element.
mouseupFired when the user releases a button on a mouse or other pointing device, while the pointer is over the element.
unloadFired when the document or a child resource are being unloaded.
wheelFired when the user rotates a mouse wheel or similar user interface component such as a touchpad.
Examples
>Mouse events
This example logs mouse events along with the X and Y coordinates at which the event was generated. Try moving the mouse into the yellow and red squares, and clicking or double-clicking.
HTML
<div> <div></div></div><div> <pre></pre> <button>Clear log</button></div>CSS
body { display: flex; gap: 1rem;}#outer { height: 200px; width: 200px; display: flex; justify-content: center; align-items: center; background-color: yellow;}#inner { height: 100px; width: 100px; background-color: red;}#contents { height: 150px; width: 250px; border: 1px solid black; padding: 0.5rem; overflow: scroll;}JavaScript
const outer = document.querySelector("#outer");const inner = document.querySelector("#inner");const contents = document.querySelector("#contents");const clear = document.querySelector("#clear");let lines = 0;outer.addEventListener("click", (event) => { log(event);});outer.addEventListener("dblclick", (event) => { log(event);});outer.addEventListener("mouseover", (event) => { log(event);});outer.addEventListener("mouseout", (event) => { log(event);});outer.addEventListener("mouseenter", (event) => { log(event);});outer.addEventListener("mouseleave", (event) => { log(event);});function log(event) { const prefix = `${String(lines++).padStart(3, "0")}: `; const line = `${event.type}(${event.clientX}, ${event.clientY})`; contents.textContent = `${contents.textContent}${prefix}${line}\n`; contents.scrollTop = contents.scrollHeight;}clear.addEventListener("click", () => { contents.textContent = ""; lines = 0;});Result
Keyboard and input events
This example logskeydown,beforeinput and,input events. Try typing into the text area. Note that keys likeShift producekeydown events but notinput events.
HTML
<textarea rows="5" cols="33"></textarea><div> <pre></pre> <button>Clear log</button></div>CSS
body { display: flex; gap: 1rem;}#story { padding: 0.5rem;}#contents { height: 150px; width: 250px; border: 1px solid black; padding: 0.5rem; overflow: scroll;}JavaScript
const story = document.querySelector("#story");const contents = document.querySelector("#contents");const clear = document.querySelector("#clear");let lines = 0;story.addEventListener("keydown", (event) => { log(`${event.type}(${event.key})`);});story.addEventListener("beforeinput", (event) => { log(`${event.type}(${event.data})`);});story.addEventListener("input", (event) => { log(`${event.type}(${event.data})`);});function log(line) { const prefix = `${String(lines++).padStart(3, "0")}: `; contents.textContent = `${contents.textContent}${prefix}${line}\n`; contents.scrollTop = contents.scrollHeight;}clear.addEventListener("click", () => { contents.textContent = ""; lines = 0;});Result
Specifications
| Specification |
|---|
| UI Events> |