JavaScript DOM KeyboardEvent property: keyCode
[this page | pdf | back links]
The keyCode property
of KeyboardEvent
objects within the JavaScript
DOM (i.e.
events triggered by an action with the keyboard) returns the Unicode character
code of the key pressed (for an onkeypress
event), or the Unicode key code of the key that triggered an onkeydown or an
onkeyup event.
It is included for compatibility only, as the latest specification recommends
using the key
property.
EXAMPLE:
HTML USED IN THIS EXAMPLE:
<!DOCTYPE html>
<html> <!-- Copyright (c) Nematrian Limited 2018 -->
<head></head>
<body>
Try pressing a key in this input box:
<input style="background-color: yellow; border: 2px solid black; width: 200px; height: 100px;"
onkeypress="myOnkeypress(event)"><br><br>
KeyboardEvent outcomes are:<br><br>
altKey: <b id="key1"></b><br>
charCode: <b id="key2"></b><br>
ctrlKey: <b id="key3"></b><br>
key: <b id="key4"></b><br>
keyCode: <b id="key5"></b><br>
location: <b id="key6"></b><br>
metaKey: <b id="key7"></b><br>
shiftKey: <b id="key8"></b><br>
which: <b id="key9"></b><br>
<script>
function myOnkeypress(event) {
document.getElementById("key1").innerHTML = event.altKey;
document.getElementById("key2").innerHTML = event.charCode;
document.getElementById("key3").innerHTML = event.ctrlKey;
document.getElementById("key4").innerHTML = event.key;
document.getElementById("key5").innerHTML = event.keyCode;
document.getElementById("key6").innerHTML = event.location;
document.getElementById("key7").innerHTML = event.metaKey;
document.getElementById("key8").innerHTML = event.shiftKey;
document.getElementById("key9").innerHTML = event.which;
}
</script>
</body>
</html>
|
NAVIGATION LINKS
Contents | Prev | Next | JavaScript DOM (and BOM)