We're looking for a user researcher to understand the needs of developers and designers. Is this you or someone you know? Check out the post: https://mzl.la/2IGzdXS



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.




var value = event.keyCode;
  • value is the Unicode value of the key that was pressed.
<!DOCTYPE html>
<html lang="en">
<title>keyCode example</title>

<script type="text/javascript">

function showKeyCode(e) {
	alert( "keyCode for the key pressed: " + e.keyCode + "\n" );


<body onkeydown="showKeyCode(event);">
<p>Press any key.</p>


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.

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

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 keyCode values associated with particular keys, run the example in Example 7: Displaying Event Object Constants and view the resulting HTML table.


Not part of W3C DOM specification. See nsIDOMKeyEvent

Document Tags and Contributors

Last updated by: Dria,