How To Detect Keypress In Javascript

 21 - Jan - 2022
 XpertPhp
 25

In this tutorial, we will explain to you how to detect keypress In Javascript. we can also know which key is pressed and what that is the Unicode value. Example

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function detectkey() {
    var unicode = event.which || event.keyCode;
    document.getElementById("keystroke").innerHTML = "The pressed key was: " + String.fromCharCode(unicode) +"<br>The Unicode value is:"+ unicode;
}
</script>
</head>
<body>
<input type="text" onkeypress="detectkey()">
<p id="keystroke"></p>
</body>
</html>
Output
The pressed key was:N
The Unicode value is:78