Mozilla.com

  1. MDC
  2. 首页
  3. DOM
  4. event.charCode

« Gecko DOM 参考

Summary

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

Syntax

value = event.charCode
  • value is the Unicode value of the character key that was pressed.

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>

Notes

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.

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

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

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

For a list of the charCode values associated with particular keys, run the example in Gecko DOM Reference:Examples #Example 7: Displaying Event Object Constants and view the resulting HTML table.

Specification

Not part of specification. See nsIDOMKeyEvent

Languages

Page last modified 07:46, 19 Jul 2007 by Mickeyboy

文件 (0)