Skip to main content

KeyboardEvent

KeyboardEvent objects describe a user interaction with the keyboard; each event describes a single interaction between the user and a key (or combination of a key with modifier keys) on the keyboard. The event type (keydown, keypress, or keyup) identifies what kind of keyboard activity occurred.

MDN Reference

Extends​

Methods​

composedPath()​

composedPath(): EventTarget[];

The composedPath() method of the Event interface returns the event's path which is an array of the objects on which listeners will be invoked. This does not include nodes in shadow trees if the shadow root was created with its ShadowRoot.mode closed.

MDN Reference

Returns​

EventTarget[]

Inherited from​

UIEvent.composedPath


getModifierState()​

getModifierState(keyArg): boolean;

The KeyboardEvent.getModifierState() method returns the current state of the specified modifier key: true if the modifier is active (that is the modifier key is pressed or locked), otherwise, false.

MDN Reference

Parameters​

ParameterType
keyArgstring

Returns​

boolean


initEvent()​

initEvent(
type,
bubbles?,
cancelable?
): void;

The Event.initEvent() method is used to initialize the value of an event created using Document.createEvent().

Parameters​

ParameterType
typestring
bubbles?boolean
cancelable?boolean

Returns​

void

Deprecated​

MDN Reference

Inherited from​

UIEvent.initEvent


initKeyboardEvent()​

initKeyboardEvent(
typeArg,
bubblesArg?,
cancelableArg?,
viewArg?,
keyArg?,
locationArg?,
ctrlKey?,
altKey?,
shiftKey?,
metaKey?
): void;

The KeyboardEvent.initKeyboardEvent() method initializes the attributes of a keyboard event object. This method was introduced in draft of DOM Level 3 Events, but deprecated in newer draft. Gecko won't support this feature since implementing this method as experimental broke existing web apps (see Firefox bug 999645). Web applications should use constructor instead of this if it's available.

Parameters​

ParameterType
typeArgstring
bubblesArg?boolean
cancelableArg?boolean
viewArg?Window | null
keyArg?string
locationArg?number
ctrlKey?boolean
altKey?boolean
shiftKey?boolean
metaKey?boolean

Returns​

void

Deprecated​

MDN Reference


initUIEvent()​

initUIEvent(
typeArg,
bubblesArg?,
cancelableArg?,
viewArg?,
detailArg?
): void;

The UIEvent.initUIEvent() method initializes a UI event once it's been created.

Parameters​

ParameterType
typeArgstring
bubblesArg?boolean
cancelableArg?boolean
viewArg?Window | null
detailArg?number

Returns​

void

Deprecated​

MDN Reference

Inherited from​

UIEvent.initUIEvent


preventDefault()​

preventDefault(): void;

The preventDefault() method of the Event interface tells the user agent that the event is being explicitly handled, so its default action, such as page scrolling, link navigation, or pasting text, should not be taken.

MDN Reference

Returns​

void

Inherited from​

UIEvent.preventDefault


stopImmediatePropagation()​

stopImmediatePropagation(): void;

The stopImmediatePropagation() method of the Event interface prevents other listeners of the same event from being called.

MDN Reference

Returns​

void

Inherited from​

UIEvent.stopImmediatePropagation


stopPropagation()​

stopPropagation(): void;

The stopPropagation() method of the Event interface prevents further propagation of the current event in the capturing and bubbling phases. It does not, however, prevent any default behaviors from occurring; for instance, clicks on links are still processed. If you want to stop those behaviors, see the preventDefault() method. It also does not prevent propagation to other event-handlers of the current element. If you want to stop those, see stopImmediatePropagation().

MDN Reference

Returns​

void

Inherited from​

UIEvent.stopPropagation

Properties​

altKey​

readonly altKey: boolean;

The KeyboardEvent.altKey read-only property is a boolean value that indicates if the alt key (Option or ⌥ on macOS) was pressed (true) or not (false) when the event occurred.

MDN Reference


AT_TARGET​

readonly AT_TARGET: 2;

Inherited from​

UIEvent.AT_TARGET


bubbles​

readonly bubbles: boolean;

The bubbles read-only property of the Event interface indicates whether the event bubbles up through the DOM tree or not.

MDN Reference

Inherited from​

UIEvent.bubbles


BUBBLING_PHASE​

readonly BUBBLING_PHASE: 3;

Inherited from​

UIEvent.BUBBLING_PHASE


cancelable​

readonly cancelable: boolean;

The cancelable read-only property of the Event interface indicates whether the event can be canceled, and therefore prevented as if the event never happened.

MDN Reference

Inherited from​

UIEvent.cancelable


cancelBubble​

cancelBubble: boolean;

The cancelBubble property of the Event interface is deprecated. Use Event.stopPropagation() instead. Setting its value to true before returning from an event handler prevents propagation of the event. In later implementations, setting this to false does nothing. See Browser compatibility for details.

Deprecated​

MDN Reference

Inherited from​

UIEvent.cancelBubble


CAPTURING_PHASE​

readonly CAPTURING_PHASE: 1;

Inherited from​

UIEvent.CAPTURING_PHASE


charCode​

readonly charCode: number;

The charCode read-only property of the KeyboardEvent interface returns the Unicode value of a character key pressed during a keypress event.

Deprecated​

MDN Reference


code​

readonly code: string;

The KeyboardEvent.code property represents a physical key on the keyboard (as opposed to the character generated by pressing the key). In other words, this property returns a value that isn't altered by keyboard layout or the state of the modifier keys.

MDN Reference


composed​

readonly composed: boolean;

The read-only composed property of the Event interface returns a boolean value which indicates whether or not the event will propagate across the shadow DOM boundary into the standard DOM.

MDN Reference

Inherited from​

UIEvent.composed


ctrlKey​

readonly ctrlKey: boolean;

The KeyboardEvent.ctrlKey read-only property returns a boolean value that indicates if the control key was pressed (true) or not (false) when the event occurred.

MDN Reference


currentTarget​

readonly currentTarget: EventTarget | null;

The currentTarget read-only property of the Event interface identifies the element to which the event handler has been attached.

MDN Reference

Inherited from​

UIEvent.currentTarget


defaultPrevented​

readonly defaultPrevented: boolean;

The defaultPrevented read-only property of the Event interface returns a boolean value indicating whether or not the call to Event.preventDefault() canceled the event.

MDN Reference

Inherited from​

UIEvent.defaultPrevented


detail​

readonly detail: number;

The UIEvent.detail read-only property, when non-zero, provides the current (or next, depending on the event) click count.

MDN Reference

Inherited from​

UIEvent.detail


DOM_KEY_LOCATION_LEFT​

readonly DOM_KEY_LOCATION_LEFT: 1;

DOM_KEY_LOCATION_NUMPAD​

readonly DOM_KEY_LOCATION_NUMPAD: 3;

DOM_KEY_LOCATION_RIGHT​

readonly DOM_KEY_LOCATION_RIGHT: 2;

DOM_KEY_LOCATION_STANDARD​

readonly DOM_KEY_LOCATION_STANDARD: 0;

eventPhase​

readonly eventPhase: number;

The eventPhase read-only property of the Event interface indicates which phase of the event flow is currently being evaluated.

MDN Reference

Inherited from​

UIEvent.eventPhase


isComposing​

readonly isComposing: boolean;

The KeyboardEvent.isComposing read-only property returns a boolean value indicating if the event is fired within a composition session, i.e., after compositionstart and before compositionend.

MDN Reference


isTrusted​

readonly isTrusted: boolean;

The isTrusted read-only property of the Event interface is a boolean value that is true when the event was generated by the user agent (including via user actions and programmatic methods such as HTMLElement.focus()), and false when the event was dispatched via EventTarget.dispatchEvent(). The only exception is the click event, which initializes the isTrusted property to false in user agents.

MDN Reference

Inherited from​

UIEvent.isTrusted


key​

readonly key: string;

The KeyboardEvent interface's key read-only property returns the value of the key pressed by the user, taking into consideration the state of modifier keys such as Shift as well as the keyboard locale and layout.

MDN Reference


keyCode​

readonly keyCode: number;

The deprecated KeyboardEvent.keyCode read-only property represents a system and implementation dependent numerical code identifying the unmodified value of the pressed key.

Deprecated​

MDN Reference


location​

readonly location: number;

The KeyboardEvent.location read-only property returns an unsigned long representing the location of the key on the keyboard or other input device.

MDN Reference


metaKey​

readonly metaKey: boolean;

The KeyboardEvent.metaKey read-only property returning a boolean value that indicates if the Meta key was pressed (true) or not (false) when the event occurred. Some operating systems may intercept the key so it is never detected.

MDN Reference


NONE​

readonly NONE: 0;

Inherited from​

UIEvent.NONE


repeat​

readonly repeat: boolean;

The repeat read-only property of the KeyboardEvent interface returns a boolean value that is true if the given key is being held down such that it is automatically repeating.

MDN Reference


returnValue​

returnValue: boolean;

The Event property returnValue indicates whether the default action for this event has been prevented or not.

Deprecated​

MDN Reference

Inherited from​

UIEvent.returnValue


shiftKey​

readonly shiftKey: boolean;

The KeyboardEvent.shiftKey read-only property is a boolean value that indicates if the shift key was pressed (true) or not (false) when the event occurred.

MDN Reference


srcElement​

readonly srcElement: EventTarget | null;

The deprecated Event.srcElement is an alias for the Event.target property. Use Event.target instead.

Deprecated​

MDN Reference

Inherited from​

UIEvent.srcElement


target​

readonly target: EventTarget | null;

The read-only target property of the Event interface is a reference to the object onto which the event was dispatched. It is different from Event.currentTarget when the event handler is called during the bubbling or capturing phase of the event.

MDN Reference

Inherited from​

UIEvent.target


timeStamp​

readonly timeStamp: number;

The timeStamp read-only property of the Event interface returns the time (in milliseconds) at which the event was created.

MDN Reference

Inherited from​

UIEvent.timeStamp


type​

readonly type: string;

The type read-only property of the Event interface returns a string containing the event's type. It is set when the event is constructed and is the name commonly used to refer to the specific event, such as click, load, or error.

MDN Reference

Inherited from​

UIEvent.type


view​

readonly view: Window | null;

The UIEvent.view read-only property returns the WindowProxy object from which the event was generated. In browsers, this is the Window object the event happened in.

MDN Reference

Inherited from​

UIEvent.view


which​

readonly which: number;

The UIEvent.which read-only property of the UIEvent interface returns a number that indicates which button was pressed on the mouse, or the numeric keyCode or the character code (charCode) of the key pressed on the keyboard.

Deprecated​

MDN Reference

Inherited from​

UIEvent.which