KeyboardEvent: KeyboardEvent() constructor
Baseline
Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The KeyboardEvent() constructor creates a new
KeyboardEvent object.
Syntax
new KeyboardEvent(type)
new KeyboardEvent(type, options)
Parameters
type-
A string with the name of the event. It is case-sensitive and browsers set it to
keydown,keyup, orkeypress. optionsOptional-
An object that, in addition of the properties defined in
UIEvent(), can have the following properties:keyOptional-
A string, defaulting to
"", that sets the value ofKeyboardEvent.key. codeOptional-
A string, defaulting to
"", that sets the value ofKeyboardEvent.code. locationOptional-
A number, defaulting to
0, that sets the value ofKeyboardEvent.location. repeatOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.repeat. isComposingOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.isComposing. charCodeOptional Deprecated-
A number, defaulting to
0, that sets the value of the deprecatedKeyboardEvent.charCode. keyCodeOptional Deprecated-
A number, defaulting to
0, that sets the value of the deprecatedKeyboardEvent.keyCode. whichOptional Deprecated-
A number, defaulting to
0, that sets the value of the deprecatedUIEvent.which. ctrlKeyOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.ctrlKey. shiftKeyOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.shiftKey. altKeyOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.altKey. metaKeyOptional-
A boolean value, defaulting to
false, that sets the value ofKeyboardEvent.metaKey.
Return value
A new KeyboardEvent object.
Specifications
| Specification |
|---|
| UI Events> # dom-keyboardevent-keyboardevent> |
Browser compatibility
Loading…
See also
KeyboardEvent, the interface of the objects it constructs.