Packagedom.events
Classpublic class KeyboardEvent
InheritanceKeyboardEvent Inheritance UIEvent Inheritance Event Inheritance Object

Introduced in: DOM 3 Events 

The KeyboardEvent interface provides specific contextual information associated with keyboard devices. Each keyboard event references a key using an identifier. Keyboard events are commonly directed at the element that has the focus.

The KeyboardEvent interface provides convenient attributes for some common modifiers keys: KeyboardEvent.ctrlKey, KeyboardEvent.shiftKey, KeyboardEvent.altKey, KeyboardEvent.metaKey. These attributes are equivalent to using the method KeyboardEvent.getModifierState(keyIdentifierArg) with "Control", "Shift", "Alt", or "Meta" respectively.

To create an instance of the KeyboardEvent interface, use the DocumentEvent.createEvent("KeyboardEvent") method call.

See also

W3C - DOM 3 Events: KeyboardEvent


Public Properties
 PropertyDefined By
  DOM 3 Events altKey : Boolean
[read-only] If the alternative (Alt) key modifier is activated.
KeyboardEvent
 InheritedDOM 2 Events bubbles : Boolean
[read-only] Used to indicate whether or not an event is a bubbling event.
Event
 InheritedDOM 2 Events cancelable : Boolean
[read-only] Indicates whether the event is cancelable or not.
Event
  Non-Standard charCode : Boolean
[read-only] Returns the Unicode value of a character key pressed during a keypress event.
KeyboardEvent
  DOM 3 Events ctrlKey : Boolean
[read-only] If the control (Ctrl) key modifier is activated.
KeyboardEvent
 InheritedDOM 2 Events currentTarget : EventTarget
[read-only] Identifies the current target for the event, as the event traverses the DOM.
Event
 InheritedDOM 3 Events defaulPrevented : Boolean
[read-only] Used to indicate whether Event.preventDefault() has been called for this event.
Event
 InheritedDOM 2 Events detail : Number
[read-only] Specifies some detail information about the Event, depending on the type of event.
UIEvent
 InheritedDOM 2 Events eventPhase : Number
[read-only] Used to indicate which phase of event flow is currently being accomplished.
Event
 InheritedNon-Standard explicitOriginalTarget : nsIDOMEventTarget
[read-only] The explicit original target of the event.
Event
 InheritedNon-Standard isChar : Boolean
[read-only] Returns a boolean indicating whether the event produced a key character or not.
UIEvent
 InheritedNon-Standard isTrusted : Boolean
[read-only] Determines if the event was from the user or script generated.
Event
  Non-Standard keyCode : DOMString
[read-only] Returns the Unicode value of a non-character key in a keypress event or any key in any other type of keyboard event.
KeyboardEvent
  DOM 3 Events keyIdentifier : DOMString
[read-only] Holds the identifier of the key.
KeyboardEvent
  DOM 3 Events keyLocation : Number
[read-only] The keyLocation attribute contains an indication of the location of they key on the device, as described in Keyboard event types.
KeyboardEvent
 InheritedNon-Standard layerX : Number
[read-only] Returns the horizontal coordinate of the event relative to the current layer.
UIEvent
 InheritedNon-Standard layerY : Number
[read-only] Returns the vertical coordinate of the event relative to the current layer.
UIEvent
  DOM 3 Events metaKey : Boolean
[read-only] If the meta (Meta) key modifier is activated.
KeyboardEvent
 InheritedDOM 3 Events namespaceURI : DOMString
[read-only] The namespace URI associated with this event at initialization time, or null if it is unspecified.
Event
 InheritedNon-Standard originalTarget : *
[read-only] The original target of the event before any retargetings.
Event
 InheritedNon-Standard pageX : Number
[read-only] Returns the horizontal coordinate of the event relative to whole document.
UIEvent
 InheritedNon-Standard pageY : Number
[read-only] Returns the vertical coordinate of the event relative to the whole document.
UIEvent
  DOM 3 Events shiftKey : Boolean
[read-only] If the shift (Shift) key modifier is activated.
KeyboardEvent
 InheritedDOM 2 Events target : EventTarget
[read-only] Used to indicate the event target.
Event
 InheritedDOM 2 Events timeStamp : DOMTimeStamp
[read-only] Used to specify the time at which the event was created in milliseconds relative to 1970-01-01T00:00:00Z.
Event
 InheritedDOM 2 Events type : DOMString
[read-only] The local name of the event type.
Event
 InheritedDOM 2 Events view : AbstractView
[read-only] Returns the AbstractView object from which the event was generated.
UIEvent
 InheritedNon-Standard which : DOMString
[read-only] Returns the numeric keyCode of the key pressed, or the character code (charCode) for an alphanumeric key pressed.
UIEvent
Public Methods
 MethodDefined By
  
DOM 3 Events getModifierState(keyIdentifierArg:DOMString):Boolean
Queries the state of a modifier using a key identifier.
KeyboardEvent
 Inherited
DOM 2 Events initEvent(typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean):void
Initializes attributes of an Event created through the DocumentEvent.createEvent method.
Event
 Inherited
DOM 3 Events initEventNS(namespaceURIArg:DOMString, typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean):void
Initializes attributes of an Event object.
Event
  
DOM 3 Events initKeyboardEvent(typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, keyIdentifierArg:DOMString, keyLocationArg:Number, modifiersListArg:DOMString):void
Initializes attributes of a KeyboardEvent object.
KeyboardEvent
  
DOM 3 Events initKeyboardEventNS(namespaceURI:DOMString, typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, keyIdentifierArg:DOMString, keyLocationArg:Number, modifiersListArg:DOMString):void
Initializes attributes of a KeyboardEvent object.
KeyboardEvent
 Inherited
DOM 2 Events initUIEvent(typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, detailArg:Number):void
Initializes attributes of an UIEvent object.
UIEvent
 Inherited
DOM 3 Events initUIEventNS(namespaceURIArg:DOMString, typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, detailArg:Number):void
Initializes attributes of an UIEvent object.
UIEvent
 Inherited
 Obsolete preventBubble():void
Prevents the event from bubbling.
Event
 Inherited
 Obsolete preventCapture():void
This method is deprecated in favor of standard stopPropagation and is removed in Gecko 1.9.
Event
 Inherited
DOM 2 Events preventDefault():void
Cancels the event if it is cancelable, without stopping further propagation of the event.
Event
 Inherited
DOM 3 Events stopImmediatePropogation():void
Prevents other event listeners from being triggered and, unlike Event.stopPropagation() its effect is immediate .
Event
 Inherited
DOM 2 Events stopPropogation():void
Prevents other event listeners from being triggered but its effect is deferred until all event listeners attached on the Event.currentTarget have been triggered .
Event
Public Constants
 ConstantDefined By
 InheritedHTML 4.0 abort : String = abort
[static] Loading of a resource has been aborted.
UIEvent
 InheritedDOM 2 Events AT_TARGET : Number = 2
[static] The current event is in the target phase, i.e.
Event
 InheritedDOM 2 Events blur : String = blur
[static] An event target loses focus.
UIEvent
 InheritedDOM 2 Events BUBBLING_PHASE : Number = 3
[static] The current event phase is the bubbling phase.
Event
 InheritedDOM 2 Events CAPTURING_PHASE : Number = 1
[static] The current event phase is the capture phase.
Event
 InheritedHTML 4.0 change : String = change
[static] A control loses the input focus and its value has been modified since gaining focus.
UIEvent
 InheritedDOM 2 Events DOMActivate : String = DOMActivate
[static] Refer to Activation requests and behavior.
UIEvent
 InheritedDOM 2 Events DOMFocusIn : String = DOMFocusIn
[static] An event target receives focus.
UIEvent
 InheritedDOM 2 Events DOMFocusOut : String = DOMFocusOut
[static] An event target loses focus.
UIEvent
  DOM 3 Events DOM_KEY_LOCATION_LEFT : Number = 1
[static] The key activated is in the left key location (there is more than one possible location for this key).
KeyboardEvent
  DOM 3 Events DOM_KEY_LOCATION_NUMPAD : Number = 3
[static] The key activation originated on the numeric keypad or with a virtual key corresponding to the numeric keypad.
KeyboardEvent
  DOM 3 Events DOM_KEY_LOCATION_RIGHT : Number = 2
[static] The key activation is in the right key location (there is more than one possible location for this key).
KeyboardEvent
  DOM 3 Events DOM_KEY_LOCATION_STANDARD : Number = 0
[static] The key activation is not distinguished as the left or right version of the key, and did not originate from the numeric keypad (or did not originate with a virtual key corresponding to the numeric keypad).
KeyboardEvent
 InheritedHTML 4.0 error : String = error
[static] A resource failed to load, or has been loaded but cannot be interpreted according to its semantics such as an invalid image, a script execution error, or non-well-formed XML.
UIEvent
 InheritedDOM 2 Events focus : String = focus
[static] An event target receives focus.
UIEvent
  DOM 3 Events keydown : String = keydown
[static] A key is pressed down.
KeyboardEvent
  DOM 3 Events keyup : String = keyup
[static] A key is released.
KeyboardEvent
 InheritedHTML 4.0 load : String = load
[static] The DOM Implementation finishes loading the resource (such as the document) and any dependent resources (such as images, style sheets, or scripts).
UIEvent
 InheritedHTML 4.0 reset : String = reset
[static] A form, such as a [HTML 4.01] or [XHTML 1.0] form, is reset.
UIEvent
 InheritedDOM 2 Events resize : String = resize
[static] A document view or an element has been resized.
UIEvent
 InheritedDOM 2 Events scroll : String = scroll
[static] A document view or an element has been scrolled.
UIEvent
 InheritedHTML 4.0 select : String = select
[static] A user selects some text.
UIEvent
 InheritedHTML 4.0 submit : String = submit
[static] A form, such as a [HTML 4.01] or [XHTML 1.0] form, is submitted.
UIEvent
 InheritedHTML 4.0 unload : String = unload
[static] The DOM implementation removes from the environment the resource (such as the document) or any dependent resources (such as images, style sheets, scripts).
UIEvent
Property Detail
DOM 3 Events altKeyproperty
altKey:Boolean  [read-only]

Introduced in: DOM 3 Events 

If the alternative (Alt) key modifier is activated.

Note: The Option key modifier on Macintosh systems must be represented using this key modifier.


Implementation
    public function get altKey():Boolean

See also


Example
         <html>
         <head>
         <title>altKey example</title>
         
         <script type="text/javascript">
         
         function showChar(e){
             alert(
                 "Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
                 + "charCode: " + e.charCode + "\n"
                 + "ALT key pressed: " + e.altKey + "\n"
             );
         }
         
         </script>
         </head>
         
         <body onkeypress="showChar(event);">
         <p>
         Press any character key,
         with or without holding down the ALT key.<br />
         You can also use the SHIFT key together with the ALT key.
         </p>
         </body>
         </html>
charCodeproperty 
charCode:Boolean  [read-only]

Non-standard (Mozilla)

Returns the Unicode value of a character key pressed during a keypress event.

For constants equivalent to these numeric codes, see KeyEvent.

Note: In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

Note: charCode is never set in the keydown and keyup events. In these cases, keyCode is set instead.

Note: To get the code of the key regardless of whether it was stored in keyCode or charCode, query the which property.

Note: Characters entered through an IME do not register through keyCode or charCode.


Implementation
    public function get charCode():Boolean

See also


Example
         <html>
         <head>
         <title>charCode example</title>
         
         <script type="text/javascript">
         
         function showChar(e) {
             alert("Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
             + "charCode: " + e.charCode);
         }
         
         </script>
         </head>
         
         <body onkeypress="showChar(event);">
         <p>Press any 'character' type key.</p>
         </body>
         </html>
DOM 3 Events ctrlKeyproperty 
ctrlKey:Boolean  [read-only]

Introduced in: DOM 3 Events 

If the control (Ctrl) key modifier is activated.


Implementation
    public function get ctrlKey():Boolean

See also


Example
         <html>
         <head>
         <title>ctrlKey example</title>
         
         <script type="text/javascript">
         
         function showChar(e) {
             alert(
                 "Key Pressed: " + String.fromCharCode(e.charCode) + "\n"
                 + "charCode: " + e.charCode + "\n"
                 + "CTRL key pressed: " + e.ctrlKey + "\n"
             );
         }
         
         </script>
         </head>
         
         <body onkeypress="showChar(event);">
         <p>Press any character key, with or without holding down the CTRL key.<br />
         You can also use the SHIFT key together with the CTRL key.</p>
         </body>
         </html>
keyCodeproperty 
keyCode:DOMString  [read-only]

Non-standard (Mozilla)

Returns the Unicode value of a non-character key in a keypress event or any key in any other type of keyboard event.

For constants equivalent to these numeric codes, see KeyEvent.

Note: In a keypress event, the Unicode value of the key pressed is stored in either the keyCode or charCode property, never both. If the key pressed generates a character (e.g. 'a'), charCode is set to the code of that character, respecting the letter case. (i.e. charCode takes into account whether the shift key is held down). Otherwise, the code of the pressed key is stored in keyCode.

Note: keyCode is always set in the keydown and keyup events. In these cases, charCode is never set.

Note: To get the code of the key regardless of whether it was stored in keyCode or charCode, query the which property.

Note: Characters entered through an IME do not register through keyCode or charCode.


Implementation
    public function get keyCode():DOMString

See also


Example
         <html>
         <head>
         <title>keyCode example</title>
         
         <script type="text/javascript">
         
         function showKeyCode(e) {
             alert("keyCode for the key pressed: " + e.keyCode + "\n");
         }
         
         </script>
         </head>
         
         <body onkeydown="showKeyCode(event);">
         <p>Press any key.</p>
         </body>
         </html>
DOM 3 Events keyIdentifierproperty 
keyIdentifier:DOMString  [read-only]

Introduced in: DOM 3 Events 

Holds the identifier of the key. The key identifiers are defined in Appendix A.2 "Key identifiers set". Implementations that are unable to identify a key must use the key identifier "Unidentified".


Implementation
    public function get keyIdentifier():DOMString

See also

DOM 3 Events keyLocationproperty 
keyLocation:Number  [read-only]

Introduced in: DOM 3 Events 

The keyLocation attribute contains an indication of the location of they key on the device, as described in Keyboard event types.


Implementation
    public function get keyLocation():Number

See also

DOM 3 Events metaKeyproperty 
metaKey:Boolean  [read-only]

Introduced in: DOM 3 Events 

If the meta (Meta) key modifier is activated.

Note: The Command key modifier on Macintosh systems must be represented using this key modifier.


Implementation
    public function get metaKey():Boolean

See also

DOM 3 Events shiftKeyproperty 
shiftKey:Boolean  [read-only]

Introduced in: DOM 3 Events 

If the shift (Shift) key modifier is activated.


Implementation
    public function get shiftKey():Boolean

See also

Method Detail
DOM 3 Events getModifierState()method
public function getModifierState(keyIdentifierArg:DOMString):Boolean

Introduced in: DOM 3 Events 

Queries the state of a modifier using a key identifier.

Parameters

keyIdentifierArg:DOMString — A modifier key identifier. Common modifier keys are "Alt", "AltGraph", "CapsLock", "Control", "Meta", "NumLock", "Scroll", or "Shift".

Note: If an application wishes to distinguish between right and left modifiers, this information could be deduced using keyboard events and KeyboardEvent.keyLocation.

Returns
Boolean — If it is a modifier key and the modifier is activated

See also

DOM 3 Events initKeyboardEvent()method 
public function initKeyboardEvent(typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, keyIdentifierArg:DOMString, keyLocationArg:Number, modifiersListArg:DOMString):void

Introduced in: DOM 3 Events 

Initializes attributes of a KeyboardEvent object. This method has the same behavior as UIEvent.initUIEvent(). The value of UIEvent.detail remains undefined.

Parameters

typeArg:DOMString — Specifies Event.type, the local name of the event type.
 
canBubbleArg:Boolean — Specifies Event.bubbles. This parameter overrides the intrinsic bubbling behavior of the event.
 
cancelableArg:Boolean — Specifies Event.cancelable. This parameter overrides the intrinsic cancelable behavior of the event.
 
viewArg:AbstractView — Specifies UIEvent.view. This value may be null.
 
keyIdentifierArg:DOMString — Specifies KeyboardEvent.keyIdentifier.
 
keyLocationArg:Number — Specifies KeyboardEvent.keyLocation.
 
modifiersListArg:DOMString — A white space separated list of modifier key identifiers to be activated on this object. As an example, "Control Alt" will mark the control and alt modifiers as activated.

See also

DOM 3 Events initKeyboardEventNS()method 
public function initKeyboardEventNS(namespaceURI:DOMString, typeArg:DOMString, canBubbleArg:Boolean, cancelableArg:Boolean, viewArg:AbstractView, keyIdentifierArg:DOMString, keyLocationArg:Number, modifiersListArg:DOMString):void

Introduced in: DOM 3 Events 

Initializes attributes of a KeyboardEvent object. This method has the same behavior as UIEvent.initUIEventNS(). The value of UIEvent.detail remains undefined.

Parameters

namespaceURI:DOMString — Specifies Event.namespaceURI, the namespace URI associated with this event, or null if no namespace.
 
typeArg:DOMString — Specifies Event.type, the local name of the event type.
 
canBubbleArg:Boolean — Specifies Event.bubbles. This parameter overrides the intrinsic bubbling behavior of the event.
 
cancelableArg:Boolean — Specifies Event.cancelable. This parameter overrides the intrinsic cancelable behavior of the event.
 
viewArg:AbstractView — Specifies UIEvent.view. This value may be null.
 
keyIdentifierArg:DOMString — Specifies KeyboardEvent.keyIdentifier.
 
keyLocationArg:Number — Specifies KeyboardEvent.keyLocation.
 
modifiersListArg:DOMString — A white space separated list of modifier key identifiers to be activated on this object. As an example, "Control Alt" will mark the control and alt modifiers as activated.

See also

Constant Detail
DOM 3 Events DOM_KEY_LOCATION_LEFTConstant
public static const DOM_KEY_LOCATION_LEFT:Number = 1

Introduced in: DOM 3 Events 

The key activated is in the left key location (there is more than one possible location for this key). Example: the left Shift key on a PC 101 Key US keyboard.

See also

DOM 3 Events DOM_KEY_LOCATION_NUMPADConstant 
public static const DOM_KEY_LOCATION_NUMPAD:Number = 3

Introduced in: DOM 3 Events 

The key activation originated on the numeric keypad or with a virtual key corresponding to the numeric keypad. Example: the '1' key on a PC 101 Key US keyboard located on the numeric pad.

See also

DOM 3 Events DOM_KEY_LOCATION_RIGHTConstant 
public static const DOM_KEY_LOCATION_RIGHT:Number = 2

Introduced in: DOM 3 Events 

The key activation is in the right key location (there is more than one possible location for this key). Example: the right Shift key on a PC 101 Key US keyboard.

See also

DOM 3 Events DOM_KEY_LOCATION_STANDARDConstant 
public static const DOM_KEY_LOCATION_STANDARD:Number = 0

Introduced in: DOM 3 Events 

The key activation is not distinguished as the left or right version of the key, and did not originate from the numeric keypad (or did not originate with a virtual key corresponding to the numeric keypad). Example: the 'Q' key on a PC 101 Key US keyboard.

See also

DOM 3 Events keydownConstant 
public static const keydown:String = keydown

Introduced in: DOM 3 Events 

A key is pressed down.

This event type is device dependent and relies on the capabilities of the input devices and how they are mapped in the operating system. This event type is generated after the keyboard mapping but before the processing of an input method editor. This event should logically happen before the event keyup is produced. Whether a keydown contributes or not to the generation of a text event is implementation dependent.

TitleValue
NamespaceNone
CancelableYes
BubblesYes
TargetElement
Context infoUIEvent.view, KeyboardEvent.keyIdentifier, KeyboardEvent.keyLocation, KeyboardEvent.altKey, KeyboardEvent.shiftKey, KeyboardEvent.ctrlKey, and KeyboardEvent.metaKey are in use.

See also

DOM 3 Events keyupConstant 
public static const keyup:String = keyup

Introduced in: DOM 3 Events 

A key is released.

This event type is device dependent and relies on the capabilities of the input devices and how they are mapped in the operating system. This event type is generated after the keyboard mapping but before the processing of an input method editor. This event should logically happen after the event keydown is produced. Whether a keyup contributes or not to the generation of a text event is implementation dependent.

TitleValue
NamespaceNone
CancelableYes
BubblesYes
TargetElement
Context infoUIEvent.view, KeyboardEvent.keyIdentifier, and KeyboardEvent.keyLocation are in use. KeyboardEvent.altKey, KeyboardEvent.shiftKey, KeyboardEvent.ctrlKey, and KeyboardEvent.metaKey are in use unless the KeyboardEvent.keyIdentifier corresponds to the key modifier itself.

See also