<div class="app">
<h2>The key Property<br>Press Any Key</h2>
<div id="keypress" class="alert"></div>
<div id="keydown" class="alert"></div>
</div>
html, body {
display: grid;
justify-items: center;
align-items: center;
font-family: sans-serif;
width: 100%;
height: 100%;
}
.app {
display: grid;
grid-template-columns: 1fr 1fr;
grid-gap: 10px;
}
h2 {
text-align:center;
grid-column: span 2;
}
h4 {
font-size: 1rem;
font-weight: normal;
margin: 0;
padding: 0;
}
.alert {
display: grid;
justify-items: center;
align-items: center;
width: 150px;
height: 150px;
background-color: rgba(0, 0, 0, 0.8);
border-radius: 5px;
color: #fff;
font-size: 2rem;
font-weight: bold;
text-align: center;
opacity: 0;
visibility: hidden;
transition: 0.5s;
}
.show {
opacity: 1;
visibility: visible;
}
@keyframes fade-in-out {
0%, 100% {
opacity: 0;
visibility: hidden;
}
1%, 99% {
opacity: 1;
visiblity: visible;
}
}
const keypress = document.getElementById('keypress')
const keydown = document.getElementById('keydown')
let timer1 = null
let timer2 = null
document.addEventListener('keypress', function(e) {
if (timer1) {
clearTimeout(timer1)
}
keypress.innerHTML = `
<div>
<h4>Keypress</h4><br>
${e.key}
</div>`
keypress.classList.add('show')
timer1 = setTimeout(function() {
keypress.classList.remove('show')
}, 5000)
})
document.addEventListener('keydown', function(e) {
if (timer2) {
clearTimeout(timer2)
}
keydown.innerHTML = `
<div>
<h4>Keydown</h4><br>
${e.key}
</div>`
keydown.classList.add('show')
timer2 = setTimeout(function() {
keydown.classList.remove('show')
}, 5000)
})
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.