The KeyboardEvent() constructor creates a new KeyboardEvent.
Syntax
event = new KeyboardEvent(typeArg, KeyboardEventInit);
Values
typeArg
Is a DOMString representing the name of the event.
KeyboardEventInitOptional
Is a KeyboardEventInit dictionary, having the following fields:
"key", optional and defaulting to"", of typeDOMString, that sets the value ofKeyboardEvent.key."code", optional and defaulting to"", of typeDOMString, that sets the value ofKeyboardEvent.code."location", optional and defaulting to0, of typeunsigned long, that sets the value ofKeyboardEvent.location."ctrlKey", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.ctrlKey."shiftKey", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.shiftKey."altKey", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.altKey."metaKey", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.metaKey."repeat", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.repeat."isComposing", optional and defaulting tofalse, of typeBoolean, that sets the value ofKeyboardEvent.isComposing."charCode", optional and defaulting to0, of typeunsigned long, that sets the value of the deprecatedKeyboardEvent.charCode."keyCode", optional and defaulting to0, of typeunsigned long, that sets the value of the deprecatedKeyboardEvent.keyCode."which", optional and defaulting to0, of typeunsigned long, that sets the value of the deprecatedKeyboardEvent.which.
The KeyboardEventInit dictionary also accepts fields from the UIEventInit and EventInit dictionaries.
Specifications
| Specification | Status | Comment |
|---|---|---|
| UI EventsThe definition of 'KeyboardEvent()' in that specification. | Working Draft | Current definition. |
| Document Object Model (DOM) Level 3 Events SpecificationThe definition of 'KeyboardEvent()' in that specification. | Obsolete | Initial definition. |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
KeyboardEvent() constructor
|
Chrome
Full support Yes |
Edge
Full support ≤79 |
Firefox
Full support 31 |
IE
No support No |
Opera
Full support Yes |
Safari
? |
WebView Android
Full support Yes |
Chrome Android
Full support Yes |
Firefox Android
Full support 31 |
Opera Android
Full support Yes |
Safari iOS
? |
Samsung Internet Android
Full support Yes |
Added code and key to KeyboardEventInit
|
Chrome
Full support 49 |
Edge
Full support ≤79 |
Firefox
? |
IE
No support No |
Opera
? |
Safari
? |
WebView Android
Full support 49 |
Chrome Android
Full support 49 |
Firefox Android
? |
Opera Android
? |
Safari iOS
? |
Samsung Internet Android
Full support 5.0 |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
See also
KeyboardEvent, the interface of the objects it constructs.
KeyboardEvent() by Mozilla Contributors is licensed under CC-BY-SA 2.5.