Unlimited Plugins, WordPress themes, videos & courses! Unlimited asset downloads! From $16.50/m
Advertisement
  1. Code
  2. Web Development

Creating a Keyboard With CSS and jQuery

by
Read Time:8 minsLanguages:

Sometimes it's just fun to play around with the programming languages we know and see what we can create. I thought it might be nice to create a little online keyboard with CSS, and then make it work with jQuery. The keyboard includes "action" keys (caps lock, shift, and delete) which dynamically changes the keyboard when clicked. I'll show you how to build it today.

Here's the final preview of the keyboard we're going to build:

Let's begin.

1. Create the Basic HTML and Files

This keyboard requires a lot of HTML setup and playing around with CSS. Each of the keys will be represented by a list item in an unordered list. Each list item will have a class attached to it, used in both the CSS and jQuery. Most of the classes are just letter, lastitem, or something similar. This will make it easy to find out which list item is which.

Make sure you have set up a folder wherever you are going to be using this keyboard. In this new folder, create an index.html file along with a css and a js folder. Finally, create a keyboard.js file in the js folder and a style.css file in the css folder.

In the HTML file, we'll be including two JavaScript files and one CSS file. Inside the body tag there will be a huge unordered list containing all the letters, numbers, and some "action" keys. The HTML will also have a textarea in it with an id of keyboard. This will be the place where all the characters are added. The below code should be placed inside the index.html file.

You won't have to worry too much about the classes on the list items for now. I'll explain them more when we're using jQuery. However, some of the classes (like right-shift and lastitem) are just there because of the CSS we'll be using.

HTML SkeletonHTML SkeletonHTML Skeleton
HTML Skeleton

2. Make the List Pretty With CSS

The JavaScript for the keyboard would work perfectly fine without any CSS, but it wouldn't look like a keyboard. I'm not going to explain every style because a lot of them are pretty self-explanatory, but there are a couple that I will go over. Save the following CSS in the style.css file located in the css folder.

Take notice of the following styles because they are very important.

  • .on: In some of the list items, there are two spans. These are the keys that have more than one character per key, like the numbers. The span with the on class will be hidden. This changed when a user clicks on the shift key, but more on that later with the JavaScript.
  • .lastitem: The last key in any row will have its right margin zeroed out so the layout won't break.
After stylingAfter stylingAfter styling
After styling

3. Bringing the Keys to Life With JavaScript

If you were to click on a list item now, nothing would happen. We're about to fix that with a little jQuery. The main idea we'll be using is to attach a click handler to each of the list items, grab the text when clicked, and do some magic to it depending on the list item's class. From here on out, all the JavaScript code will go into the keyboard.js file.

The Setup

We need to open up jQuery and define three variables that will be used through the code. These variables are the textarea, a shift status, and a caps lock status.

What comes next is attaching the click handler to all the list items (keys). Two variables are set up when the key is clicked. $this is defined just to require less typing from us, and character is defined as the HTML of the list item. If the list item is a letter, nothing will happen to this variable, and it will be returned.

The Shift Key and Caps Lock

If the shift key (list items with the class of left-shift or right-shift) is clicked, we want to toggle the uppercase class of each letter. Then, for the list items with a class of symbol, we want to toggle the display between the nested span tags. What we want to do next is set shift to the opposite boolean value (if it's true set it to false, and vice versa), and the caps lock variable to false, and finally return false so as not to do anything else with the character variable.

Now, if the caps lock key is clicked, we will toggle the uppercase class on letter list items, set the caps lock variable to true, and return false.

The Delete Key

For the delete key, we need to assign another variable: html—the contents of what's currently in the textarea. Once we have that, we set the new HTML of the textarea to everything but the last character. This is done with JavaScript's substr method. Once again, we return false so as not to run anything else.

DeleteDeleteDelete
Delete

Special Characters

For all the list items which aren't a letter and aren't one of the "actions" keys, we change the character variable to something special. For a list item with the symbol class, character is set to the contents of whatever span is visible. A space character is (obviously) used for the space bar. The tab character is represented by \t, and finally a new line (the return key) is \n.

Special CharactersSpecial CharactersSpecial Characters
Special Characters

Uppercase Letters

If you can remember when we handled the shift and caps lock keys, an uppercase class was either added or removed using the toggleClass function. If the uppercase class is found, the character is converted to its uppercase form with the help of the toUpperCase method.

The Aftermath

On a normal keyboard, you usually only need the shift key for one letter. If the shift variable is found to be set to true, we want to toggle the display of the symbol's spans. What also happens is that if the caps lock key is "on", the letters are once again toggled between uppercase and lowercase.

To finish off, the character is added to the textarea, and the user can continue "typing".

KeyboardKeyboardKeyboard
The completed keyboard

Conclusion

Sometimes it's nice to play around, even if the final product isn't truly "real world." By applying a few classes to our list items, we were able to create a keyboard powered by CSS and jQuery. The keyboard isn't totally useless. I've seen websites where there's an option for an on-screen keyboard. But mostly, this allows us to gain a better understanding of the capabilities of CSS and jQuery.

This post has been updated with contributions from Kingsley Ubah. Kingsley is passionate about creating content that educates and inspires readers. Hobbies include reading, football and cycling.

Advertisement
Did you find this post useful?
Advertisement
Looking for something to help kick start your next project?
Envato Market has a range of items for sale to help get you started.