Create a virtual keyboard that users can interact with on the webpage.
Create a virtual keyboard that users can interact with on the webpage.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Virtual Keyboard</title>
<style>
body {
font-family: 'Arial', sans-serif;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
margin: 0;
background-color: #f4f4f4;
}
#keyboard {
display: grid;
grid-template-columns: repeat(10, 1fr);
gap: 5px;
max-width: 500px;
margin-bottom: 20px;
}
button {
padding: 10px;
font-size: 16px;
border: 1px solid #ccc;
background-color: #fff;
cursor: pointer;
}
#inputField {
font-size: 16px;
padding: 10px;
width: 300px;
margin-bottom: 20px;
}
</style>
</head>
<body>
<input type="text" id="inputField" readonly>
<div id="keyboard"></div>
<script>
document.addEventListener('DOMContentLoaded', function () {
const keyboard = document.getElementById('keyboard');
const inputField = document.getElementById('inputField');
const keys = [
'1', '2', '3', '4', '5', '6', '7', '8', '9', '0',
'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P',
'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L',
'Z', 'X', 'C', 'V', 'B', 'N', 'M',
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '-',
'Tab', 'Shift', 'Control', 'Enter', 'Backspace',
'↑', '←', '↓', '→'
];
keys.forEach(key => {
const button = document.createElement('button');
button.textContent = key;
button.addEventListener('click', () => onKeyPress(key));
keyboard.appendChild(button);
});
function onKeyPress(key) {
switch (key) {
case 'Tab':
inputField.value += '\t';
break;
case 'Shift':
// Handle Shift key
break;
case 'Control':
// Handle Control key
break;
case 'Enter':
inputField.value += '\n';
break;
case 'Backspace':
inputField.value = inputField.value.slice(0, -1);
break;
case '↑':
// Handle up arrow key
break;
case '←':
// Handle left arrow key
break;
case '↓':
// Handle down arrow key
break;
case '→':
// Handle right arrow key
break;
default:
inputField.value += key;
break;
}
}
});
</script>
</body>
</html>

More in 21 days of HTML CSS JS
See all →- Ultimate Guide to Free Web Development Resources In today’s digital age, web development is a crucial skill that opens up numerous opportunities. Whether you’re a beginner looking to build...
- 10 Effective Ways to Center a Div Hello, myself Ajink. In this article, we will explore the various techniques to center a div element on a web page. Centering a div might se...
- Learn HTML HTML Document Structure: : Defines the root of an HTML document. : Contains meta-information about the document. : Sets the title of the doc...
- How to Create an Valentine’s Day Website? How to Create an Valentine’s Day Website , lets get started Demo : Here is Complete SourceCode : body background-color: #fde8e8; font-family...
- Create a accordion with html css and javascript body { font-family: 'Arial', sans-serif; margin: 0; padding: 0; display: flex; justify-content: center; align-items: center; height: 100vh;...
- Create a virtual canvas where users can draw and paint using HTML5 canvas and JavaScript. Hey , I am Ajink in this post we will create a virtual canvas where you can draw and paint using html5 and javascript ! Virtual Canvas body...