How To Detect Keypress In Javascript

 21 - Jan - 2022

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>
<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;
<input type="text" onkeypress="detectkey()">
<p id="keystroke"></p>
The pressed key was:N
The Unicode value is:78