Make keyboard using HTML + JS with fully working keys, make design sleek and modern
- User
Make keyboard using HTML + JS with fully working keys, make design sleek and modern
- Assistant
Make keyboard using HTML + JS with fully working keys, make design sleek and modern
Creating a virtual keyboard using HTML, CSS, and JavaScript is a great way to enhance user interaction on your website or web application. Below is a step-by-step guide to building a fully functional, sleek, and modern virtual keyboard.
We'll create:
Let's get started!
First, set up the basic HTML structure.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Virtual Keyboard</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="keyboard-container">
<input type="text" id="textInput" placeholder="Type here..." readonly>
<div id="keyboard" class="keyboard">
<!-- Keys will be generated here -->
</div>
</div>
<script src="script.js"></script>
</body>
</html>
Explanation:
keyboard-container: A container for the input field and the keyboard.textInput: An input field where the typed text will appear.keyboard: A div where we'll render the keys.Create a style.css file to style the keyboard.
/* General Styling */
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f0f2f5;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
/* Keyboard Container */
.keyboard-container {
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
box-shadow: 0 8px 15px rgba(0, 0, 0, 0.1);
}
/* Input Field */
#textInput {
width: 100%;
padding: 15px;
font-size: 18px;
margin-bottom: 20px;
border: 2px solid #dfe1e5;
border-radius: 8px;
outline: none;
}
/* Keyboard Styling */
.keyboard {
display: grid;
grid-template-columns: repeat(14, 1fr);
grid-gap: 5px;
}
/* Keys Styling */
.key {
padding: 15px;
border: none;
background-color: #e3e3e3;
border-radius: 5px;
font-size: 16px;
cursor: pointer;
user-select: none;
transition: background-color 0.2s;
}
.key:active,
.key:hover {
background-color: #d1d1d1;
}
/* Special Keys Styling */
.key.wide {
grid-column: span 2;
}
.key.extra-wide {
grid-column: span 3;
}
Explanation:
Create a script.js file to handle the keyboard logic.
// Key layout for the keyboard
const keyLayout = [
'1','2','3','4','5','6','7','8','9','0','Backspace',
'Q','W','E','R','T','Y','U','I','O','P',
'CapsLock','A','S','D','F','G','H','J','K','L','Enter',
'Shift','Z','X','C','V','B','N','M',',','.','?','Shift',
'Space'
];
const keyboardContainer = document.getElementById('keyboard');
const textInput = document.getElementById('textInput');
let capsLock = false;
function createKeys() {
keyLayout.forEach(key => {
const keyElement = document.createElement('button');
keyElement.setAttribute('type', 'button');
keyElement.classList.add('key');
keyElement.textContent = key;
switch(key) {
case 'Backspace':
keyElement.classList.add('wide');
keyElement.addEventListener('click', backspace);
break;
case 'CapsLock':
keyElement.classList.add('extra-wide');
keyElement.addEventListener('click', toggleCapsLock);
break;
case 'Enter':
keyElement.classList.add('wide');
keyElement.addEventListener('click', enter);
break;
case 'Shift':
keyElement.classList.add('extra-wide');
keyElement.addEventListener('click', toggleCapsLock);
break;
case 'Space':
keyElement.classList.add('extra-wide');
keyElement.style.gridColumn = 'span 7';
keyElement.addEventListener('click', space);
break;
default:
keyElement.addEventListener('click', () => {
let char = key;
if (capsLock) {
char = char.toUpperCase();
} else {
char = char.toLowerCase();
}
textInput.value += char;
});
}
keyboardContainer.appendChild(keyElement);
});
}
function backspace() {
textInput.value = textInput.value.substring(0, textInput.value.length - 1);
}
function enter() {
textInput.value += '\n';
}
function space() {
textInput.value += ' ';
}
function toggleCapsLock() {
capsLock = !capsLock;
const keys = document.querySelectorAll('.key');
keys.forEach(key => {
if (key.textContent.length === 1) {
key.textContent = capsLock ? key.textContent.toUpperCase() : key.textContent.toLowerCase();
}
});
}
// Initialize the keyboard
createKeys();
Explanation:
createKeys(): Function that creates button elements for each key and adds event listeners.capsLock state and updates the key labels accordingly.Now, when you open index.html in a browser, you should see a sleek, modern virtual keyboard. Clicking on the keys should input characters into the text field.
To make it even sleeker:
Include Font Awesome in your HTML head:
<head>
<!-- Existing Head Content -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
</head>
Update JavaScript for Icons:
// Inside createKeys function, replace switch cases for special keys
switch(key) {
case 'Backspace':
keyElement.classList.add('wide');
keyElement.innerHTML = '<i class="fas fa-backspace"></i>';
keyElement.addEventListener('click', backspace);
break;
case 'Enter':
keyElement.classList.add('wide');
keyElement.innerHTML = '<i class="fas fa-level-down-alt"></i>';
keyElement.addEventListener('click', enter);
break;
// ... rest of the code remains the same
}
Note: Remember to update the CSS if needed to align the icons properly.
You've now created a fully functional virtual keyboard with a sleek and modern design. This keyboard can be further customized and integrated into your projects as needed. Experiment with different styles and functionalities to make it fit your specific needs.
Feel free to ask if you need further assistance or have questions about customizing the keyboard!
Not yet included in a published release.