Click here to Skip to main content
13,795,145 members
Click here to Skip to main content
Add your own
alternative version


32 bookmarked
Posted 12 Dec 2011
Licenced CPOL

Virtual keyboard - Secure keying of password

, 12 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Virtual keyboard - can be used for secure keying of passwords.


Secure login for bank sites also provide a virtual keyboard to enter the password in a public system. This project provides an implementation for a set pattern.


Using the code

The requirements for such a keyboard would be:

  1. Show all keys which can be used for the password field. This would include alphabets, numbers, and special characters.
  2. On click of each key, convert all keys to # so that somebody looking over the shoulder will not be able to figure it out.
  3. Provide Caps Lock, backspace, and Clear buttons.

Since all keys are similar in function apart from the value, we can use direct HTML and a template to create the bulk of the keyset.

<input type="button" value="a" id="btn1"/>

This would give us a button, however we want more. Hence we add the following events:

  • onmousedown - change it to #
  • onmouseup - restore key value
  • onclick - the complete action of clicking - write the character to the password box

This can be held by a variable for ease of use later on:

string buttonHtmlString = "<input type=\"button\" class=\"keys\" 
  value=\"{0}\" name =\"{1}\" id=\"btnVirKey\" 
  onclick=\"func_click(this.value);\" onmousedown = \"hashthekeys();\" 
  onmouseout=\"unhashthekeys();\" onmouseup=\"unhashthekeys();\" />";

Another aspect is, we are using up some screen area for displaying this, and hence we have to program for a fixed pattern of keys.

I have selected a format where the keys look like:

/*      row1 - 15 char - alphabets
 *      row2 - 11 char - alphabets
 *      row3 - 10 char - numbers + 2 char symbols
 *      row4 - 15 char - symbols
 *      row5 - 15 char - symbols
 *      row6 - Back Space, Clear, Caps Lock

Hence, I am going to set a specific pattern to fit my area and randomize to that extent. You will have to find out how many keys fit into the screen area and then decide on it. I decided to keep the width of my table to 300px, and on trial error basis, I have a max of 15 characters per line.

Generating the keys

I have created three lists, for each set of different characters I need.

List<string> listAlphabets = new List<string>();
List<string> listNumbers = new List<string>();
List<string> listSymbols = new List<string>();

I fill them up, programmatically or manually, as appeals to me.

private void CreateAlphabets()
    for (int i =97; i <= 122; i++)
        //a = 97; z=122

private void CreateNumberlist()
    for (int i = 0; i < 10; i++)

private void CreateSymbolList()
    string[] symbollist = new string[] {"!","@","#","$","%","^","&",

Initially, adding all keys to a list and then removing them on insertion will do the trick.

I add a row after the fifteenth button to fit with the pattern. The same goes with the other sets - numbers, symbols.

//Filling first 2 rows -Alphabets
for (int i = 0; i < 26; i++)
    int next = rand.Next(1,listAlphabets.Count) -1;
                   listAlphabets[next], listAlphabets[next]));
    if(i== 14)
        //adding the second row

For the JavaScript, we have to declare a form and have a common button name to enable ease of looping to set and reset the values. We need a swap field for storing the actual value - name can be used for this.

We just append the value when the onclick event triggers:

function func_click(charVal) {   
    finalresult = document.getElementById("txtoutput").value;
    document.getElementById("txtoutput").value = finalresult + charVal;

The hashing and unhashing - since all the button names are the same, FormName.ButtonName[index] is the easiest way to set and reset the button values.

function hashthekeys() {
 for (var z = 0; z < document.formVitualKeyboard.btnVirKey.length; z++) {
            document.formVitualKeyboard.btnVirKey[z].value = '#';
 for (var z = 0; z < document.formVitualKeyboard.button2.length; z++) {
    document.formVitualKeyboard.button2[z].value = '#';
function unhashthekeys() {
 for (var z = 0; z < document.formVitualKeyboard.btnVirKey.length; z++) {
    document.formVitualKeyboard.btnVirKey[z].value = 
 for (var z = 0; z < document.formVitualKeyboard.button2.length; z++) {
    document.formVitualKeyboard.button2[z].value = 

Backspace, Clear, and Caps Lock have custom functions, and the buttons are also defined separately.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHelp!! Pin
GeorgeHLord18-Oct-13 21:40
memberGeorgeHLord18-Oct-13 21:40 
GeneralMy vote of 5 Pin
jawed.ace13-Dec-11 1:00
memberjawed.ace13-Dec-11 1:00 
GeneralMy vote of 5 Pin
Member 825477112-Dec-11 21:17
memberMember 825477112-Dec-11 21:17 
GeneralVery good example. Useful. Pin
sudhansu_k12312-Dec-11 20:19
membersudhansu_k12312-Dec-11 20:19 
GeneralMy vote of 3 Pin
sudhansu_k12312-Dec-11 20:18
membersudhansu_k12312-Dec-11 20:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web05 | 2.8.181207.3 | Last Updated 12 Dec 2011
Article Copyright 2011 by DestinyCoder007
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid