Virtual Keyboard JavaScript Source Code

Virtual Keyboard JavaScript Source Code
Code Snippet:Simple Virtual Keyboard - JS
Author: vaishak101
Published: January 10, 2024
Last Updated: January 22, 2024
Downloads: 1,202
License: MIT
Edit Code online: View on CodePen
Read More

This JavaScript source code implements a simple virtual keyboard on a webapge. The keyboard allows users to type alphanumeric characters, toggle caps lock, add spaces, and activate a light effect. It responds to user clicks on the virtual keys and updates a text area accordingly. This code is helpful for creating virtual keyboards in web applications.

You can use this code in web applications, online forms, or text editors to provide users with a virtual keyboard. It benefits users by offering an alternative input method, improving accessibility for touch devices.

How to Create a Virtual Keyboard Using JavaScript

1. First, add the following HTML structure to your web page. It includes the necessary HTML elements. You can customize the appearance and placement of the virtual keyboard as needed.

<div class="wrapper">
      <h1 class="heading">Simple Virtual Javascript Keyboard</h1>
      <textarea
        name="textarea"
        id=""
        cols="30"
        rows="5"
        class="text"
        readonly
      ></textarea>
      <div class="keyboard__body">
        <div class="container">
          <div class="row">
            <button class="key content">1</button
            ><button class="key content">2</button
            ><button class="key content">3</button
            ><button class="key content">4</button
            ><button class="key content">5</button
            ><button class="key content">6</button
            ><button class="key content">7</button
            ><button class="key content">8</button
            ><button class="key content">9</button
            ><button class="key content">0</button
            ><button class="key backSpace">&larr;</button>
          </div>
          <div class="row">
            <button class="key alpha content">q</button
            ><button class="key alpha content">w</button
            ><button class="key alpha content">e</button
            ><button class="key alpha content">r</button
            ><button class="key alpha content">t</button
            ><button class="key alpha content">y</button
            ><button class="key alpha content">u</button
            ><button class="key alpha content">i</button
            ><button class="key alpha content">o</button
            ><button class="key alpha content">p</button>
          </div>
          <div class="row">
            <button class="key alpha content">a</button
            ><button class="key alpha content">s</button
            ><button class="key alpha content">d</button
            ><button class="key alpha content">f</button
            ><button class="key alpha content">g</button
            ><button class="key alpha content">h</button
            ><button class="key alpha content">j</button
            ><button class="key alpha content">k</button
            ><button class="key alpha content">l</button>
          </div>
          <div class="row">
            <button class="key alpha content">z</button
            ><button class="key alpha content">x</button
            ><button class="key alpha content">c</button
            ><button class="key alpha content">v</button
            ><button class="key alpha content">b</button
            ><button class="key alpha content">n</button
            ><button class="key alpha content">m</button>
          </div>
          <div class="row">
            <button class="key caps">Caps</button
            ><button class="key spaceBar"></button
            ><button class="key light">
              <ion-icon name="sunny-outline" class="light"></ion-icon>
            </button>
          </div>
        </div>
      </div>
    </div>
    <script
      type="module"
      src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.esm.js"
    ></script>
    <script
      nomodule
      src="https://unpkg.com/ionicons@5.5.2/dist/ionicons/ionicons.js"
    ></script>

2. The following CSS code is essential for styling the virtual keyboard. Ensure that you include this CSS code in your HTML file or link to an external CSS file.

.container {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  padding: 30px 0;
}
@keyframes lights {
  0% {
    color: red;
    box-shadow: 0 5px 15px red;
  }
  25% {
    color: goldenrod;
    box-shadow: 0 5px 15px goldenrod;
  }
  50% {
    color: blue;
    box-shadow: 0 5px 15px blue;
  }
  75% {
    color: rgb(35, 201, 201);
    box-shadow: 0 5px 15px rgb(35, 201, 201);
  }
  100% {
    color: red;
    box-shadow: 0 5px 15px red;
  }
}
.heading{
  text-align:center;
  font-weight:100;
}
.keyboard__body {
  margin: 0 auto;
  width: 1000px;
  background-color: #40514E;
  border-radius: 15px;
}
.key {
  font-size:18px;
  width: 60px;
  height: 60px;
  border-radius: 15px;
  border: none;
  background-color: #F3F6F6;
}
.key:active {
  transform: translateY(2px);
  box-shadow: 0 0px 0px transparent;
}
.row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
}
.spaceBar {
  width: 500px;
}
.spaceBar:active {
  transform: translateY(2px);
}
.text {
  margin: 0 auto;
  width: 500px;
  display: block;
  margin-bottom: 50px;
  border: 2px solid lime;
}
.text:focus {
  outline: none;
}
.lightOn {
  animation: lights 8s linear infinite;
}

4. Finally, include the JavaScript code in your HTML file using <script> tags. This code contains the core functionality of the virtual keyboard. It handles key presses, caps lock, and a light effect.

'use strict';
const container = document.querySelector('.container');
const textarea = document.querySelector('.text');
const alphabet = document.querySelectorAll('.alpha');
const key = document.querySelectorAll('.key');
let checkCaps = false;
let checkLights = false;
//CAPSLOCK
const caps = function () {
  if (checkCaps == false) {
    for (let i = 0; i < alphabet.length; i++) {
      let getAlpha = alphabet[i].textContent;
      const up = getAlpha.toUpperCase();
      alphabet[i].textContent = up;
    }
    checkCaps = true;
  } else {
    for (let i = 0; i < alphabet.length; i++) {
      let getAlpha = alphabet[i].textContent;
      const low = getAlpha.toLowerCase();
      alphabet[i].textContent = low;
    }
    checkCaps = false;
  }
};
//Lights
const blink = function () {
  if (checkLights == false) {
    key.forEach((key) => key.classList.add('lightOn'));
    checkLights = true;
  } else {
    key.forEach((key) => key.classList.remove('lightOn'));
    checkLights = false;
  }
};
//Event listener
container.addEventListener('click', function (e) {
  if (e.target.classList.contains('key')) {
    if (e.target.classList.contains('spaceBar')) {
      textarea.textContent += ' ';
    }
    if (e.target.classList.contains('caps')) {
      caps();
    }
    if (e.target.classList.contains('content')) {
      const getContent = e.target.textContent;
      textarea.textContent += getContent;
    }
    if (e.target.classList.contains('backSpace')) {
      textarea.textContent = textarea.textContent.substring(
        0,
        textarea.textContent.length - 1
      );
    }
    if (e.target.classList.contains('light')) {
      blink();
    }
  } else {
    return;
  }
});

You can customize the keyboard layout and appearance by modifying the HTML and CSS. To change the keys or their styles, update the relevant HTML elements and CSS rules.

That’s all! hopefully, you have successfully created Virtual Keyboard Javascript Source Code. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X