Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Properties
 keyCode Property

  Switch on low bandwidth view
keyCode Property

Sets or retrieves the Unicode key code associated with the key that caused the event.

Syntax

[ sKeyCode = ] object.keyCode

Possible Values

sKeyCodeInteger that specifies or receives the Unicode key code.

The property is read/write. The property has no default value.

DHTML expressions can be used in place of the preceding value(s). As of Internet Explorer 8, expressions are not supported in IE8 mode. For more information, see About Dynamic Properties.

Remarks

The property is used with the onkeydown, onkeyup, and onkeypress events.

The property's value is 0 if no key caused the event.

Standards Information

There is no public standard that applies to this property.

Applies To

event, Event Constructor
Tags What's this?: event (x) Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Beware of different keyCode values      D_W ... yecril   |   Edit   |   Show History

keyCode returns the "virtual-key code" from the WM_KEY* messages in the Windows API when checked in the keydown and keyup events, and the ASCII/Unicode character codes in the keypress event.

For example, the DELETE key has a hex value of 007F (127 decimal) in both Unicode and ASCII. If I inspect window.event.keyCode in the keyup event, after pressing the DELETE key, the property returns a value of 46 (decimal). The DELETE key does not generate keypress events. 46 happens to be the VKey code for a delete key on my US keyboard. (VKey codes are defined in WinUser.h.)

Likewise, the keyCode also returns 65 in the keydown and keyup events regardless of whether the character entered is an "A" or an "a". However, the keyCode actually does return a 97 for "a" and 65 for "A" in the keypress event. So beware the internal inconsistency of keyCode, depending on during which event it is inspected.
Some keyboard layouts, like German and French, rearrange virtual key codes; others simply reassign character values to them, leaving key codes intact. The Polish keyboard layout reassigns AltGr to Ctrl+Alt (two consecutive codes).

Flag as ContentBug
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker