Hello Coder! Welcome to the Codewithrandom blog. In this blog, we learn how to create a Keyboard Counter(when the user presses any key from the keyboard its counts and shows on the screen). We use Html, Css, and JavaScript for creating this Keyboard Counter.
I hope you enjoy our blog so let's start with a basic Html Structure for a Keyboard Hit Counter.
HTML Code For Keyboard Counter:-
<div id="activity">
<img src="http://code.quirkbot.com/assets/images/logo/white-outline.svg" width="30%" alt="">
<h1 id="counter"><span class="hits">0</span>
<h1>Keys Hit</h1>
</div>
There is all the Html code for the Keyboard Hit Counter. Now, you can see output without Css and JavaScript. then we write Css and JavaScript for the Keyboard Hit Counter.
CSS Code For Keyboard Counter:-
body {
margin: 0;
font-family: "Open Sans", comic-sans;
position: absolute;
width: 100vw;
height: 100vh;
overflow: hidden;
display: table;
}
#activity {
display: table-cell;
text-align: center;
vertical-align: middle;
}
#activity:before {
content: "Hit Any Key On Your Keyboard";
position: absolute;
top: -1;
left: 0;
width: 100vw;
height: 10vh;
background: rgba(10, 10, 10, 10, 10);
}
#activity:after {
content: "Codewithrandom";
position: absolute;
bottom: 0;
left: 0;
width: 100vw;
height: 12vh;
}
#result {
text-transform: uppercase;
}
a:link,
a:hover,
a:visited,
a:active {
text-decoration: bold;
}
.hits {
font-size: 5.75em;
font-weight: bolder;
}
Keyboard Counter JavaScript Code:-
var hits = 0;
var hitElement = document.querySelector(".hits");
document.body.onkeyup = function (e) {
if ((e.keyCode == 1, 2, 3, 4, 5, 6, 7, 8, 9, 10)) {
addHit();
}
};
var addHit = function () {
hits++;
renderHits();
};
var renderHits = function () {
hitElement.innerHTML = hits;
};
Now we have completed our Keyboard Counter. Hope you like the Keyboard Counter Project. you can see the output video and project screenshots. See our other blogs and gain knowledge in front-end development.
Thank you!
Written by - Code With Random/Anki
Codepen by - Braxtoony
Top comments (1)
This is such an insightful post! I really appreciate how you broke down the topic in a way that's easy to understand. Looking forward to implementing some of these tips in my daily routine!
Preschools in Koramangala