How to detect key code using JavaScript

Hello friends, today we will be going to Detect key code using Html CSS and JavaScript. I have posted many videos and articles before related to the JavaScript project, now this the something new that we are going to build.

Video Tutorial of How to Detect key code using Html CSS and JavaScript


As you have seen on the given video tutorial of  How to Detect key code using Html CSS and JavaScript, 

If you are feeling bored watching the given video tutorial of How to Detect key code using Html CSS and JavaScript then you can copy or download the given codes below:


How to Detect key code using Html CSS and JavaScript | Free Source Code

To create this program (Detect Key). First, you need to create two Files one HTML File and another one is CSS File. After creating these files just paste the following codes in your file.

In the first place, make a HTML document with the name of index.html and glue the given codes in your HTML record. Keep in mind, you've to make a document with .html extension.
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<link rel="stylesheet" href="./style.css">
	<title>Detect Key using javascript</title>
	<!-- S-Tech04 -->
</head>
<body>
	<div class="insert">
		<div class="key">Press any key to get Code</div>
	</div>

	<script src="./script.js"></script>

</body>
</html>
Second, make a CSS record with the name of style.css and glue the given codes in your CSS document. Keep in mind, you've to make a record with .css extension.
*
{
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: sans-serif;
}
body
{
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    background: #008cff;
}
.key
{
    display: inline-flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    font-size: 20px;
    background: #f5f2f2;
    border: 1px solid #000;
    padding: 20px;
    font-weight: bold;
    position: relative;
    margin: 20px;
    min-width: 150px;
}
.key small
{
    position: absolute;
    top: -25px;
    left: 0;
    width: 100%;
    font-size: 14px;
    color: #000;
    text-align: center;
}
last, create a JavaScript file with the name of script.js and paste the given codes in your JavaScript file. Remember, you’ve to create a file with .js extension.
const insert = document.querySelector('.insert');

window.addEventListener('keydown', (event)=>{
    insert.innerHTML = `
        <div class="key">
        ${event.key === " " ? "Space" : event.key}
        <small>Event.key</small>
        </div>
        <div class="key">
        ${event.keyCode}
        <small>Event.keyCode</small>
        </div>
        <div class="key">
        ${event.code}
        <small>Event.Code</small>
        </div>
    `
})
That’s all, now you’ve successfully created a program to Detect key code using Html CSS and JavaScript. If your code doesn’t work or you’ve faced any error/problem, please download the source code files from the given download button. It’s free and a .zip file will be downloaded then you’ve to extract it.
Click on the following download button to download all source code files.


Download Now
Please Wait...
Direct Link click here.

How can I help you

Post a Comment

How can I help you

Post a Comment (0)

Previous Post Next Post