This article is in need of a technical review.
KeyboardEvent objects describe a user interaction with the keyboard. Each event describes a key; the event type (keydown, keypress, or keyup) identifies what kind of activity was performed.
KeyboardEvent indicates just what's happening on a key. When you need to handle text input, use HTML5 input event instead. For example, if user inputs text from hand-writing system like tablet PC, key events may not be fired.Method overview
boolean getModifierState(in DOMString keyArg); DOM level 3 |
void initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString charArg, in DOMString keyArg, in unsigned long locationArg, in DOMString modifiersListArg, in boolean repeat); |
Attributes
| Attribute | Type | Description |
altKey |
boolean |
true if the Alt (or Option, on Mac) key was active when the key event was generated. Read only. |
char |
DOMString |
The character value of the key. If the key corresponds to a printable character, this value is a non-empty Unicode string containing that character. If the key doesn't have a printable representation, this is an empty string. See key names for the detail. Read only.
Note: If the key is used as a macro that inserts multiple characters, this attribute's value is the entire string, not just the first character.
Warning: This has been dropped from DOM Level 3 Events. This is supported only on IE.
|
charCode |
unsigned long |
The Unicode reference number of the key; this attribute is used only by the
Warning: This attribute is deprecated; you should use
key instead, if available. |
KeyboardEvent.code DOM level 3 |
DOMString |
The code value of the key represented by the event. See KeyboardEvent.code for the detail. |
ctrlKey |
boolean |
true if the Control key was active when the key event was generated. Read only. |
isComposing DOM level 3 |
boolean |
true if the event is fired between after compositionstart and before compositionend. Read only. |
KeyboardEvent.key DOM level 3 |
DOMString |
The key value of the key represented by the event. See |
KeyboardEvent.keyCode |
|
A system and implementation dependent numerical code identifying the unmodified value of the pressed key. Read only. See the document of
Warning: This attribute is deprecated; you should use
key instead, if available. |
locale |
DOMString |
A locale string indicating the locale the keyboard is configured for. This may be the empty string if the browser or device doesn't know the keyboard's locale. Read only.
Note: This does not describe the locale of the data being entered. A user may be using one keyboard layout while typing text in a different language.
|
unsigned long |
The location of the key on the keyboard or other input device; see Key location constants below. Read only. |
|
metaKey |
boolean |
true if the Meta (or Command, on Mac) key was active when the key event was generated. Read only. |
repeat DOM level 3 Requires Gecko 28.0 |
boolean |
true if the key is being held down such that it is automatically repeating. Read only. |
shiftKey |
boolean |
true if the Shift key was active when the key event was generated. Read only. |
which |
unsigned long |
A system and implementation dependent numeric code identifying the unmodified value of the pressed key; this is usually the same as
Warning: This attribute is deprecated; you should use
key instead, if available. |
Constants
Virtual key codes
| Constant | Value | Description |
DOM_VK_* |
Predefined keyCode values. See "Constants for keyCode" section of KeyboardEvent.keyCode document. |
Key location constants
These constants describe the location on the keyboard of key events. DOM level 3
| Constant | Value | Description |
DOM_KEY_LOCATION_STANDARD |
0 | The key must not be distinguished between the left and right versions of the key, and was not pressed on the numeric keypad or a key that is considered to be part of the keypad. |
DOM_KEY_LOCATION_LEFT |
1 | The key was the left-hand version of the key; for example, this is the value of the location attribute when the left-hand Control key is pressed on a standard 101 key US keyboard. This value is only used for keys that have more that one possible location on the keyboard. |
DOM_KEY_LOCATION_RIGHT |
2 | The key was the right-hand version of the key; for example, this is the value of the location attribute when the right-hand Control key is pressed on a standard 101 key US keyboard. This value is only used for keys that have more that one possible location on the keyboard. |
DOM_KEY_LOCATION_NUMPAD |
3 |
The key was on the numeric keypad, or has a virtual key code that corresponds to the numeric keypad.
Note: When NumLock is locked, Gecko always returns
DOM_KEY_LOCATION_NUMPAD for the keys on NumPad. Otherwise, when NumLock is unlocked and the keyboard actually has a numeric keypad, Gecko always returns DOM_KEY_LOCATION_NUMPAD too. On the other hand, if the keyboard doesn't have a keypad, such as on a notebook computer, some keys become Numpad only when NumLock is locked. When such keys fires key events, the location attribute value depends on the key. That is, it must not be DOM_KEY_LOCATION_NUMPAD.
Note: NumLock key's key events indicate
DOM_KEY_LOCATION_STANDARD both on Gecko and Internet Explorer. |
DOM_KEY_LOCATION_MOBILE |
4 |
The key was on a mobile device; this can be on either a physical keypad or a virtual keyboard.
Note: Gecko always returns
DOM_KEY_LOCATION_MOBILE on Android (Prior to 18), Maemo, and Boot to Gecko.
Warning: This value has been dropped from the latest D3E spec. Gecko will drop this support. bug 936313
|
DOM_KEY_LOCATION_JOYSTICK |
5 |
The key was a button on a game controller or a joystick on a mobile device.
Note: Gecko never fires trusted key events with
DOM_KEY_LOCATION_JOYSTICK except on Android. Starting 18, native key events on Android may have this value.
Warning: This value has been dropped from the latest D3E spec. Gecko will drop this support. bug 936313
|
Methods
getModifierState()
See KeyboardEvent.getModifierState.
initKeyboardEvent()
Deprecated
This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.
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 bug 999645). Web applications should use constructor instead of this if it's available.
void initKeyboardEvent( in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString charArg, in DOMString keyArg, in unsigned long locationArg, in DOMString modifiersListArg, in boolean repeat );
Parameters
-
typeArg -
The type of keyboard event; this will be one of
keydown,keypress, orkeyup. -
canBubbleArg - Whether or not the event can bubble.
-
cancelableArg - Whether or not the event can be canceled.
-
viewArg - ?
-
charArg - The value of the char attribute.
-
keyArg - The value of the key attribute.
-
locationArg - The value of the location attribute.
-
modifiersListArg - A whitespace-delineated list of modifier keys that should be considered to be active on the event's key. For example, specifying "Control Shift" indicates that the user was holding down the Control and Shift keys when pressing the key described by the event.
-
repeatArg - The value of the repeat attribute.
There are keydown, keypress, and keyup events. For most keys, Gecko dispatches a sequence of key events like this:
- When the key is first depressed, the
keydownevent is sent. - If the key is not a modifier key, the
keypressevent is sent. - When the user releases the key, the
keyupevent is sent.
Special cases
Certain keys toggle the state of an LED indicator, such as Caps Lock, Num Lock, and Scroll Lock. On Windows and Linux, these keys dispatch only the keydown and keyup events. Note that on Linux, Firefox 12 and earlier also dispatched the keypress event for these keys.
On Mac, however, Caps Lock dispatches only the keydown event due to a platform event model limitation. Num Lock had been supported on old MacBook (2007 model and older) but Mac hasn't supported Num Lock feature even on external keyboards in these days. On the old MacBook which has Num Lock key, Num Lock doesn't cause any key events. And Gecko supports Scroll Lock key if an external keyboard which has F14 is connected. However, it generates keypress event. This inconsistent behavior is a bug; see bug 602812.
Auto-repeat handling
When a key is pressed and held down, it begins to auto-repeat. This results in a sequence of events similar to the following being dispatched:
keydownkeypresskeydownkeypress- <<repeating until the user releases the key>>
keyup
This is what the DOM Level 3 specification says should happen. There are some caveats, however, as described below.
Auto-repeat on some GTK environments such as Ubuntu 9.4
In some GTK-based environments, auto-repeat dispatches a native key-up event automatically during auto-repeat, and there's no way for Gecko to know the difference between a repeated series of keypresses and an auto-repeat. On those platforms, then, an auto-repeat key will generate the following sequence of events:
keydownkeypresskeyupkeydownkeypresskeyup- <<repeating until the user releases the key>>
keyup
In these environments, unfortunately, there's no way for web content to tell the difference between auto-repeating keys and keys that are just being pressed repeatedly.
Auto-repeat handling prior to Gecko 4.0
Before Gecko 4.0 , keyboard handling was less consistent across platforms.
- Windows
- Auto-repeat behavior is the same as in Gecko 4.0 and later.
- Mac
- After the initial keydown event, only keypress events are sent until the keyup event occurs; the inter-spaced keydown events are not sent.
- Linux
- The event behavior depends on the specific platform. It will either behave like Windows or Mac depending on what the native event model does.
Specification
Example
<!DOCTYPE html>
<html>
<head>
<script>
var metaChar = false;
var exampleKey = 16;
function keyEvent(event) {
var key = event.keyCode || event.which;
var keychar = String.fromCharCode(key);
if (key == exampleKey) {
metaChar = true;
}
if (key != exampleKey) {
if (metaChar) {
alert("Combination of metaKey + " + keychar);
metaChar = false;
} else {
alert("Key pressed " + key);
}
}
}
function metaKeyUp (event) {
var key = event.keyCode || event.which;
if (key == exampleKey) {
metaChar = false;
}
}
</script>
</head>
<body onkeydown="keyEvent(event)" onkeyup="metaKeyUp(event)">
</body>
</html>
""""Browser compatibility
| Feature | Chrome | Firefox (Gecko) | Internet Explorer | Opera | Safari (WebKit) |
|---|---|---|---|---|---|
| Basic support | (Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
| constructor | (Yes) | 31.0 (31.0) | Not supported | (Yes) | ? |
.char |
Not supported | Not supported | (Yes) | Not supported | Not supported |
.charCode |
(Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
.code |
See Browser compatibility of KeyboardEvent.code. |
||||
.isComposing |
Not supported | 31.0 (31.0) | Not supported | Not supported | Not supported |
.key |
See Browser compatibility of KeyboardEvent.key. |
||||
.keyCode |
(Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
.locale |
Not supported | Not supported | (Yes) | Not supported | Not supported |
.location |
(Yes) | 15.0 (15.0) | (Yes) | Not supported | Not supported |
.repeat |
Not supported | 28.0 (28.0) | (Yes) | Not supported | Not supported |
.which |
(Yes) | (Yes) | (Yes) | (Yes) | (Yes) |
.getModifierState() |
See Browser compatibility of KeyboardEvent.getModifierState |
||||
.initKeyboardEvent() |
(Yes) *1 | Not supported *2 | 9.0 *3 | ? | (Yes) *1 |
| Feature | Android | Firefox Mobile (Gecko) | IE Phone | Opera Mobile | Safari Mobile | |
|---|---|---|---|---|---|---|
| Basic support | ? | (Yes) | ? | ? | ? | |
| constructor | ? | ? | ? | ? | ||
.char |
? | Not supported | ? | ? | ? | |
.charCode |
? | (Yes) | ? | ? | ? | |
.code |
See Browser compatibility of KeyboardEvent.code. |
|||||
.isComposing |
Not supported | Not supported | Not supported | Not supported | ||
.key |
See Browser compatibility table of KeyboardEvent.key. |
|||||
.keyCode |
? | (Yes) | ? | ? | ? | |
.locale |
? | Not supported | ? | ? | ? | |
.location |
? | 15.0 (15.0) | ? | ? | ? | |
.repeat |
? | 31.0 (31.0) | ? | ? | ? | |
.which |
? | (Yes) | ? | ? | ? | |
.getModifierState() |
See Browser compatibility of KeyboardEvent.getModifierState |
|||||
.initKeyboardEvent() |
? | Not supported | ? | ? | ? | |
*1 The arguments of initKeyboardEvent() of WebKit and Blink's are different from the definition in DOM Level 3 Events. The method is: initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyIndentifierArg, in unsigned long locationArg, in boolean ctrlKeyArg, in boolean altKeyArg, in boolean shiftKeyArg, in boolean metaKeyArg, in boolean altGraphKeyArg)
*2 Gecko won't support initKeyboardEvent() because supporting it completely breaks feature detection of web applications. See bug 999645.
*3 The argument of initKeyboardEvent() of IE is different from the definition in DOM Level 3 Events. The method is: initKeyboardEvent(in DOMString typeArg, in boolean canBubbleArg, in boolean cancelableArg, in views::AbstractView viewArg, in DOMString keyArg, in unsigned long locationArg, in DOMString modifierListArg, in boolean repeatArt, in DOMString locationArg). See document of initKeyboardEvent() in MSDN.