keyup
and keydown
give you information about the physical key that was pressed.
JavaScript keyup keycode Example
HTML example code. Using onkeyup property to call function.
<!DOCTYPE html>
<html>
<head>
<script>
function myFunction(event) {
alert ("You pressed!");
}
</script>
</head>
<body>
<input type="text" onkeyup="myFunction(event)">
</body>
</html>
Output:
addEventListener keyup example
<!DOCTYPE html>
<html>
<body>
<script>
document.addEventListener("keyup", function(e) {
alert(e.code);
});
</script>
</body>
</html>
Do comment if you have any doubts or suggestions on this JS keycode tutorial.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version