Detect Key Presses & Key Code using JavaScript

Detect Key Presses & Key Code using JavaScript

Detect Key Presses & Key Code using JavaScript

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Detect Key Presses and Key Code Of Pressed Key. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Detect Key Presses & Key Code Project.

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Detect Key Presses & Key Code Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Detect Key Presses & Key Code Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Detect Key Presses & Key Code Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Detect Key Presses & Key Code

First we’ll start with creating the structure of the Random Hex Generator project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Detect Key Presses</title>
    <!-- Google Fonts -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@500&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet-->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="result">Press Any Key To Get Started</div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

50+ HTML, CSS & JavaScript Projects With Source Code

CSS Code for Detect Key Presses & Key Code

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Random Hex Generator project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now lets code the JavaScript part to make responsive.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  font-family: "Poppins", sans-serif;
}
body {
  height: 100vh;
  background: linear-gradient(#2887e3 50%, #16191e 50%);
}
#result {
  background-color: #242831;
  width: 80vw;
  max-width: 600px;
  position: absolute;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  padding: 80px 40px;
  text-align: center;
  color: #f5f5f5;
  font-size: 3.6vmin;
  border-radius: 10px;
  box-shadow: 0 10px 50px rgba(0, 0, 0, 0.2);
  font-style: italic;
}
#result span:nth-child(1) {
  color: #2887e3;
  font-size: 3em;
  display: block;
  font-style: normal;
}
#result span:nth-child(2) {
  font-size: 1.5em;
  font-style: normal;
}

JavaScript Code for Detect Key Presses & Key Code

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. Let us see the Final Output of the project Detect Key Presses & Key Code using HTML, CSS & JavaScript (Source Code).

window.addEventListener("keydown", (e) => {
  document.getElementById(
    "result"
  ).innerHTML = `The key pressed is <span>${e.key}</span><span>Key Code: ${e.keyCode}</span>`;
});a

Creating A Password Generator Using Javascript

Final Output


We have Successfully created our Detect Key Presses & Key Code using HTML, CSS & JavaScript (Source Code). You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search code with random on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply