Get the key code from key event in JavaScript
Description
The following code shows how to get the key code from key event.
Example
<html>
<head>
<title>Key Events Example</title>
<script type="text/javascript">
function handleEvent(oEvent) {<!-- ww w . ja v a 2 s .com-->
var oTextbox = document.getElementById("txt1");
oTextbox.value += "\n>" + oEvent.type;
oTextbox.value += "\n target is " + (oEvent.target || oEvent.srcElement).id;
oTextbox.value += "\n keyCode is " + oEvent.keyCode;
}
</script>
</head>
<body>
<P>Type some characters into the first textbox.</p>
<P><textarea id="txtInput" rows="15" cols="50"
onkeypress="handleEvent(event)"></textarea></p>
<P><textarea id="txt1" rows="15" cols="50"></textarea></p>
</body>
</html>
The code above generates the following result.
Javascript Tutorial Key Event
Check if Alt key is pressed during the even...
Check if Shift key is pressed during the ev...
Check if the Control key is pressed during ...
Convert the key code from key event to char...
Get key event target element id in JavaScri...
Get the key char from the key event in Java...
Listen to body on key down event in JavaScr...
Listen to key down event for TextArea in Ja...
Listen to key down event for input text ele...
Listen to key press event for TextArea in J...
Listen to key up event for TextArea in Java...
Check if Alt key is pressed during the even...
Check if Shift key is pressed during the ev...
Check if the Control key is pressed during ...
Convert the key code from key event to char...
Get key event target element id in JavaScri...
Get the key char from the key event in Java...
Get the key code from key event in JavaScri...
Get the key event target or event source in...Listen to body on key down event in JavaScr...
Listen to key down event for TextArea in Ja...
Listen to key down event for input text ele...
Listen to key press event for TextArea in J...
Listen to key up event for TextArea in Java...