JavaScript detecting which key is pressed

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

You might find yourself in a situation where certain keypresses might do something for your application or game.
Today we'll be looking at how we can detect which key is pressed in JavaScript.
The end result is this cool little playground:

Let's start with the basics. We will need a way for JavaScript to be aware any key is pressed.
document.onkeydown = function(e) {
console.log('key down');
console.log(e);
};
This will log all key down events, which is what we are looking for.
The e variable will contain the actual KeyBoardEvent, and it has quite the information inside.

There are a couple things we can use that are helpful in there.
Knowing that, let's make a cool visual tool that will output these three elements for the user.
I'm going to be using Tailwind to make a quick styled application, the main setup will be:
<body class="my-auto mx-auto bg-gray-100">
<div class="max-w-md py-4 px-8 bg-white shadow-lg rounded-lg my-20">
<div class="flex justify-center -mt-16 hidden">
<div
class="w-20 h-20 object-cover rounded-full border-2 border-indigo-500 flex items-center justify-center bg-white text-3xl"
id="keyCodeLarge"
></div>
</div>
<div>
<p class="text-gray-600" id="info">
Press any key to see the magic 🪄
</p>
<p class="mt-4 text-gray-600 hidden">key: <span id="key"></span></p>
<p class="mt-2 text-gray-600 hidden">code: <span id="code"></span></p>
<p class="mt-2 text-gray-600 hidden">keyCode: <span id="keyCode"></span></p>
</div>
</div>
</body>
As you might have noted, I've added some ids based on which we will add the represented value with JavaScript.
I've also added an information paragraph for when we don't have any keypress yet.
We start by defining the variables we are going to be needing.
const key = document.getElementById('key'),
code = document.getElementById('code'),
keyCode = document.getElementById('keyCode'),
keyCodeLarge = document.getElementById('keyCodeLarge'),
info = document.getElementById('info'),
hiddenElements = document.querySelectorAll('.hidden');
This is a mix of the key information we will place and the hidden fields we need to show.
Now in our keyDown function, we can act on this and place the right information.
document.onkeydown = function(e) {
[].forEach.call(hiddenElements, function(el) {
el.classList.remove('hidden');
});
info.classList.add('hidden');
key.innerHTML = e.key;
code.innerHTML = e.code;
keyCode.innerHTML = e.keyCode;
keyCodeLarge.innerHTML = e.keyCode;
};
That is as simple as it gets!
You can try it out in this Codepen.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter