Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

JavaScript Virtual Keyboard

0.00/5 (No votes)
30 Mar 2008 59  
This article presents a Virtual Keyboard - an important addendum to the library of usability tools.
Virtual keyboard bound to a TEXTAREA field

Introduction

Imagine that you are sitting in a London Internet cafe wishing to write an e-mail to your family living in Athens. It's good if someone in your family speaks English. If not, where would you find a keyboard with a Greek layout? I'm sure you can recall a dozen situations when you thought, "I wish I had another keyboard." This article presents the Virtual Keyboard that solves this usability problem. The design task for it can be specified as follows:

  • Allow text input from computers without the user's native language layout installed, therefore allowing the creation of national and multilingual interfaces -- e.g. Web-based e-mail -- that can be used worldwide.
  • Allow text input from computers without keyboards or with sensor screens -- hand-held PCs, smartphones, etc. -- or with remote controls such as mice, e-pens, etc. being the only input devices.
  • Protect users from keylogger-type spyware.

Installation of the Virtual Keyboard requires a casual knowledge of HTML and JavaScript. To be able to fine-tune the script, you must be familiar with W3C DOM Level 1, CSS Level 1 and the DOM/Microsoft Internet Explorer event model. Virtual Keyboard is an open-source script distributed under the zlib/libpng license.

Setup

Six easy steps:

  1. Download the source archive.

  2. Choose your installation:

    • vkboard.js (1-vkboard folder in the archive) is the primary script. Full keyboard is simulated as follows:

      Screenshot - jvk-full.jpg

    • vkboards.js (2-vkboard_slim folder) is almost the same as previous, but the language menu, which can be accessed by clicking on the red rectangle in the left-bottom corner of the keyboard, has a special configuration. The cells are arranged in rows, not as a simple drop-down menu like in the previous variant.

      Screenshot - jvk-slim.jpg

      This installation is recommended if you're short on space (UMPC, touchscreen kiosk, etc.) or if you have 4 or more layouts installed.

    • vnumpad.js (3-numpad_full folder) is the numpad part of the keyboard.

      numpad variant

    • vatmpad.js (4-numpad_atm folder) is a stripped numpad that contains only the Enter and number keys.

      numpad_atm variant

  3. Include a reference to the chosen script file in your HTML page:

    <HTML>
        <HEAD>
    
           <SCRIPT type="text/javascript" src="vkboard.js"></SCRIPT>
    
           ...
    
        </HEAD>
     ...

    Note that for each type of installation, two script files are available:

    • vkboard.js /vkboards.js /vnumpad.js /vatmpad.js are the original script. If you wish to change the script or just want to learn how it works, this is the file for you to look at.
    • vkboardc.js /vkboardsc.js /vnumpadc.js /vatmpadc.js is a compressed version of the script, respectively 30.5%/30.5%/39.8%/39.5% smaller than the original. This is the file you should use on the Web.
  4. Define a callback function:

    <HTML>
        <HEAD>
    
            <SCRIPT type="text/javascript" src="vkboard.js"></SCRIPT>
    
            <SCRIPT>
    
            // Minimal callback function:
            function keyb_callback(char)
            {
                // Let's bind vkeyboard to the <TEXTAREA>
    
                // with id="textfield":
                var text =
                    document.getElementById("textfield"), val = text.value;
    
                switch(ch)
                {
                    case "BackSpace":
                    var min=(val.charCodeAt(val.length - 1) == 10) ? 2 : 1;
                    text.value = val.substr(0, val.length - min);
                    break;
    
                   case "Enter":
                       text.value += "\n";
                       break;
    
                   default:
                       text.value += ch;
                }
            }
    
            </SCRIPT>
    
        </HEAD>
        ...

    The callback function must have one or two parameters. The first parameter is mandatory and accepts a string value returned by the vkeyboard script. The second parameter is optional and accepts the id of the container of the vkeyboard that called the callback. It may be useful if you bind a single callback function to multiple vkeyboards. Note that this is the most basic callback function. An example of a more advanced code snippet is given later.

  5. Define a container for the keyboard, which must be an empty DIV or SPAN.

     <HTML>
    
        ...
    
        <BODY>
    
            ...
    
            <TEXTAREA id="textfield" rows="10" cols="50"></TEXTAREA>
    
            <DIV id="keyboard"></DIV>
    
        </BODY>
    
    </HTML>
  6. Finally, show the keyboard.

     <BODY onload="new VKeyboard("keyboard", keyb_callback);">
    
     <!-- VKeyboard is shown on load, bound to container with
          id="keyboard" and with callback function "keyb_callback" -->

    Of course, the creation of VKeyboard -- numpad: VNumpad, ATM-style numpad: VATMpad -- can be used anywhere a JavaScript code can be.

API

The VKeyboard constructor has a myriad of parameters to help you control its look and feel. Here is the full list, together with parameter defaults.

var vkb =
    new VKeyboard("keyboard",   // container's id, mandatory
                  keyb_callback,// reference to callback function, mandatory
                                // (this & following parameters are optional)
                  true,         // create the arrow keys or not?
                  true,         // create up and down arrow keys?
                  false,        // reserved
                  true,         // create the numpad or not?
                  "",           // font name ("" == system default)
                  "14px",       // font size in px
                  "#000",       // font color
                  "#F00",       // font color for the dead keys
                  "#FFF",       // keyboard base background color
                  "#FFF",       // keys' background color
                  "#DDD",       // background color of switched/selected item
                  "#777",       // border color
                  "#CCC",       // border/font color of "inactive" key
                                // (key with no value/disabled)
                  "#FFF",       // background color of "inactive" key
                                // (key with no value/disabled)
                  "#F77",       // border color of language selector's cell
                  true,         // show key flash on click? (false by default)
                  "#CC3300",    // font color during flash
                  "#FF9966",    // key background color during flash
                  "#CC3300",    // key border color during flash
                  false,        // embed VKeyboard into the page?
                  true,         // use 1-pixel gap between the keys?
                  0);           // index (0-based) of the initial layout

Please be careful when upgrading vkeyboard from earlier versions. Always check the number/flow of the parameters. VNumpad and VATMpad have a limited set of parameters:

var vkb =
    new VNumpad("numpad",     // container's id, mandatory
                pad_callback, // reference to the callback function, mandatory
                "",           // font name ("" == system default)
                              // (this and following parameters are optional)
                "14px",       // font size in px
                "#000",       // font color
                "#FFF",       // keyboard base background color
                "#FFF",       // keys' background color
                "#777",       // border color
                true,         // show key flash on click? (false by default*)
                "#CC3300",    // font color for flash event
                "#FF9966",    // key background color for flash event
                "#CC3300",    // key border color for flash event
                false,        // embed VNumpad into the page?
                true);        // use 1-pixel gap between the keys?

Note that although "flash switch" is off by default, it is switched on in all of the provided samples. VKeyboard has the following public methods:

  • Show: Shows or hides the whole vkeyboard. It has a single optional parameter, a boolean value that states whether to show or hide.
    vkb.Show(true);  // Shows the vkeyboard
    vkb.Show(false); // Hides the vkeyboard
    vkb.Show();      // Shows the vkeyboard again. Action is by default.
  • ShowNumpad (full and slim variants only): Shows or hides the numpad. It has a single optional parameter, a boolean value that dictates whether to show or hide.
    vkb.ShowNumpad(true);  // Shows the numpad
    vkb.ShowNumpad(false); // Hides the numpad
    vkb.ShowNumpad();      // Shows the numpad again. This action is by default.
  • """"set-param"""">SetParameters: Allows you to adjust vkeyboard parameters at runtime. The function accepts an even number of values in pairs: the vkeyboard parameter name and parameter value.
    // Adjusting single parameter:
    vkb.SetParameters("font-color", "#F0A050");
    // Adjusting two parameters at once:
    vkb.SetParameters("font-name", "Arial", "font-color", "#F0A050");

    Valid parameter names are:

    • callback - callback function reference. Valid parameter value: reference to any function with 1 or 2 formal parameters
    • font-name - name of keyboard font. Valid parameter value: font name or empty string (= default font)
    • font-size - font size in pixels. Valid parameter value: font size in pixels
    • font-color - font color. Valid parameter value here and further on: string in format of #xxxxxx or #xxx, where x is any valid hexadecimal digit
    • dead-color - color of the dead keys
    • base-color - color of the keyboard base
    • key-color - color of the keys
    • selection-color - color of: selected item in the language menu or active modifier key
    • border-color - color of the key border
    • inactive-border-color - border color of the disabled key
    • inactive-key-color - background color of the disabled key
    • lang-cell-color - border color of the language menu item
    • click-font-color - border color of the language menu item
    • click-key-color - border color of the language menu item
    • click-border-color - border color of the language menu item
    • show-click - show key flash on click. Valid parameter value: true or false
    • layout - index (0-based integer) of the layout to switch to. Use it to programmatically switch layouts

Test Suite

The script comes with the following samples:

  1. 1-edit-simple.html: The most basic installation of the JavaScript Virtual Keyboard.

  2. 2-edit-full.html: Almost the same as above, but the keyb_callback function is not so simple as it was. While simple installation only appends or deletes characters from the end of the text in a TEXTAREA, this sample allows you to edit text in the way you're used to with any other text processor. This sample shows how the JavaScript Virtual Keyboard should normally be used. All other samples are derived from this one.

  3. 3-edit-translator.html: This one is useful if you do have the keyboard, but it doesn't have your native layout on its keys. Simply run this installation, switch vkeyboard to your native layout and type with a real keyboard, not a mouse. The script will substitute the typed characters with those chosen in vkeyboard.

  4. 4-test-change.html: Sample illustrates the use of the SetParameters API. Watch how vkeyboard changes its color.

  5. 5-test-fly.html: Sample shows how to handle multiple INPUT fields with a single vkeyboard.

  6. 6-test-fly-anonym.html: Almost the same as above, but INPUT fields are allowed to omit the id property. Can be very useful for introducing vkeyboard into existing sites/pages where page changes are undesirable.

  7. 7-test-any-css.html: Sample shows how to set the vkeyboard font size with units other than pixels. A special function, convert_to_px, is used to convert arbitrary unit strings to pixel-based values. The following units are allowed: px, %, em, ex, pt, pc, cm, mm and in. Note that the convert_to_px function is still quite experimental. The main problem is that only the Microsoft Internet Explorer browser provides a way -- via the window.screen.logicalXDPI property -- to retrieve the current screen DPI setting, which is used to convert from absolute to relative length units. With any other browser, only px, %, em and ex can be used safely. A "usual" setting of 96 dots per inch -- quite common for Windows machines -- is used if other length units are specified.

  8. 8-test-scale.html: This sample shows how to sidestep the font scaling problem in Mozilla and Microsoft Internet Explorer browsers. The problem with Microsoft Internet Explorer is that it only zooms, i.e. changes font size, on elements that do not have the font-size style set explicitly. The Mozilla browser has a similar problem: it does change the font-size, but does not change the dimensions of the text's container element. In this sample, script tracks the changes to the base font-size and uses the SetParameters API to appropriately scale the vkeyboard layout. This sample should be viewed only in Mozilla (Firefox) and Microsoft Internet Explorer. Opera browser implements a smart zoom -- i.e. it just zooms the entire page -- and thus does not suffer from the problem. You can also overcome this trouble with one of the Microsoft Internet Explorer's wrapper browsers. For example, MyIE2 (Maxthon) also implements a smart zoom. There are also rumors that the Firefox 3 browser will feature an Opera-like page zoom behaviour.

Note that all of the above samples can be found in the vkboard folder of the source archive. Variants other than the full keyboard are not provided with the full test suite, only with a single sample that shows the most simple installation of every variant.

Creating Your Own Language Layout

Two easy steps:

  • Append the avail_langs array with a two-member array consisting of the language abbreviation -- usually an ISO 639-1 language code -- and the layout name written in that language, using Unicode hex values where required.
    avail_langs:
        [["Us", "English (US)"],
            ["Ca", "Canadian"],
            ["Ru", "&#x0420;&#x0443;&#x0441;" +
            "&#x0441;&#x043A;&#x0438;&#x0439;"],
            ["De", "Deutsch"],
            ["Fr", "Fran&#x00E7;ais"],
            ["Es", "Espa&#x00F1;ol"],
            ["It", "Italiano"],
            ["Cz", "&#x010C;esky"],
            ["El", "&#x0388;&#x03BB;&#x03BB;" +
            "&#x03B7;&#x03BD;&#x03B1;&#x03C2;"],
            ["He", "&#x05E2;&#x05D1;&#x05E8;&#x05D9;&#x05EA;"]];
  • Define "normal" and, optionally, the "Caps Lock"ed, "Shift"ed, "AltGr"ed and "AltGr+Shift"ed arrays. The following rules apply:
    • Each array's name must begin with the language abbreviation and the underscore symbol.
    • Names of arrays with values representing symbols for a keyboard with "Caps Lock" pressed must end with caps.
      // Czech layout:
      Cz_caps: ["&#x003B;", ... , "&#x002D;"];
    • Names of arrays with values representing symbols for a keyboard with "Shift" pressed must end with shift.
      Cz_shift: ["&#x00BA;", ... , "&#x005F;"];
    • Names of arrays with values representing symbols for a keyboard with "AltGr" pressed must end with alt_gr.
      Cz_alt_gr: [..., "&#x0021;", "&#x002F;"];
    • Names of arrays with values representing symbols for a keyboard with the "AltGr" and "Shift" keys pressed must end with alt_gr_shift.
      Cz_alt_gr_shift: ["&#x007E;", ... , "&#x003F;"];
    • Names of arrays with values representing symbols for a keyboard's normal condition -- i.e. with no modifier keys pressed -- must end with normal.
      Cz_normal: ["&#x003B;", ... , "&#x002D;"];
    • Each array must have exactly 48 entries, each containing either the hexadecimal value of the appropriate symbol or, in the case of a dead key, the array consisting of the hex value for this dead symbol and the name of one of the following predefined arrays:
      • Acute (?)
      • Breve (?)
      • Caron (?)
      • Cedilla (?)
      • Circumflex (^)
      • DialytikaTonos (?, dialytika tonos, combined acute + umlaut)
      • DotAbove (?, dot above)
      • DoubleAcute (?, double acute)
      • Grave (`)
      • Ogonek (?)
      • RingAbove (°, ring above)
      • Tilde (~)
      • Umlaut (?)

      Also, the Macron (¯) diacritic array is available via the macron.txt file in the 1-vkboard folder of the archive. It is not included to the main script because no layout I know implements it as a dead key.

      Cz_alt_gr: [["&#x0060;", "Grave"], // dead key
          "&#x0021;", ...                // simple key
    • Arrays are mapped to the layout according to the following illustration, where numbers within cells are the array indices.

      array-to-key mapping

    • The "normal" array is mandatory; others are optional.

    The following layouts are built into the full and slim keyboard variants. The number/index next to the layout name is the layout index in the IBM Globalization database:

    • English (US International) - 103P
    • Canadian (multilingual standard) - taken from Wikipedia article on keyboard layouts
    • German - 129
    • French - 189
    • Spanish - 173
    • Italian - 142
    • Russian - 443
    • Czech - 243
    • Greek - 319
    • Hebrew - 212

    24 other layouts are available in a separate layout pack that can be found in the 5-layout_pack folder in the archive. It includes:

    Please see the readme.txt file in the 5-layout_pack folder in the archive for instructions regarding these layouts.

Creating Your Own Keyboard Layout

You may wish to create a custom key layout. There are two ways to achieve this:

  1. Use atm.js as a template; it is the simplest script of four. See the numpad_atm folder in the archive for more details. In short, the script flow is as follows:

    • Create the outer box:

      var initX = 0, initY = 0;
      
      ...
      
      var kb = document.createElement("DIV");
      ct.appendChild(kb);
      
      ct.style.display = "block";
      ct.style.position = "absolute";
      ct.style.top = initY + "px", ct.style.left = initX +"px";
      
      kb.style.position = "relative";
      kb.style.top = "0px", kb.style.left = "0px";
      kb.style.border = "1px solid " + bc;
      
      var kb_main = document.createElement("DIV");
      kb.appendChild(kb_main);
      
      kb_main.style.position = "relative";
      kb_main.style.width = "1px";
      kb_main.style.cursor = "default";
      kb_main.style.backgroundColor = bkc;
      
      ...
    • Create the keys with the _setup_key method:

      var kb_pad_7 = this._setup_key(kb_main, "1px",
          "1px", cp, cp, bc, c, lh, fs);
      kb_pad_7.sub.innerHTML = "7";
      kb_pad_7.sub.id = container_id + "___pad_7";
    • Route all output to the _generic_callback_proc method. For the VATMpad object, this is done in the _set_key_state method while refreshing the layout:

      this._setup_event(key_sub, 'mousedown',
          this._generic_callback_proc);
      
    • Invoke the callback function. This happens when the user "presses" a "key":

      _generic_callback_proc: function(event)
      {
          ...
      
          if(val && vkboard.Callback)
              vkboard.Callback(val);
      }

      Sample layout

  2. Hire me. It is the best way to get a custom modification of a JavaScript Virtual Keyboard, specialized tuning and optimizations, pro support and fast, authoritative answers to JavaScript programming questions.

Call from Beyond

One natural feature that all users expect from a text field is the ability to edit the text at any position within a field. However, it is impossible to do so with a function described earlier, which only appends symbols to or removes from the end of the text. The following script is an attempt to write a compatible callback function to fulfill the described task. It is largely based on the discussion in the thescripts.com forum.

<HEAD>
    <SCRIPT type="text/javascript"><!--

    var opened = false,
        insertionS = -1,  // selection start

        insertionE =  0;  // selection end


    var userstr = navigator.userAgent.toLowerCase();
    var safari = (userstr.indexOf('applewebkit') != -1);
    var gecko  = (userstr.indexOf('gecko') != -1) && !safari;
    var standr = gecko || window.opera || safari;

    ...

    // Advanced callback function:
    //

    function keyb_callback(ch)
    {
        var val = text.value;

        switch(ch)
        {
            case "BackSpace":
            if(val.length)
            {
                var span = null;

                if(document.selection)
                span = document.selection.createRange().duplicate();

                if(span && span.text.length > 0)
                {
                    span.text = "";
                    getCaretPositions(text);
                }
                else
                deleteAtCaret(text);
            }
            break;

            case "<":
                if(insertionS > 0)
                setRange(text, insertionS - 1, insertionE - 1);
            break;

            case ">":
                if(insertionE < val.length)
                setRange(text, insertionS + 1, insertionE + 1);
            break;

            case "/\\":
                if(!standr) break;
                var prev  = val.lastIndexOf("\n", insertionS) + 1;
                var pprev = val.lastIndexOf("\n", prev - 2);
                var next  = val.indexOf("\n", insertionS);

                if(next == -1) next = val.length;
                var nnext = next - insertionS;

                if(prev > next)
                {
                    prev  = val.lastIndexOf("\n", insertionS - 1) + 1;
                    pprev = val.lastIndexOf("\n", prev - 2);
                }

                // number of chars in current line to the left of the caret:
                var left = insertionS - prev;

                // length of the prev. line:
                var plen = prev - pprev - 1;

                // number of chars in the prev. line to
                // the right of the caret:
                var right = (plen <= left) ? 1 : (plen - left);

                var change = left + right;
                setRange(text, insertionS - change, insertionE - change);

            break;

            case "\\/":
                if(!standr) break;

                var prev  = val.lastIndexOf("\n", insertionS) + 1;
                var next  = val.indexOf("\n", insertionS);
                var pnext = val.indexOf("\n", next + 1);

                if( next == -1)  next = val.length;
                if(pnext == -1) pnext = val.length;

                if(pnext < next) pnext = next;

                if(prev > next)
                prev  = val.lastIndexOf("\n", insertionS - 1) + 1;

                // number of chars in current line to the left of the caret:
                var left = insertionS - prev;

                // length of the next line:
                var nlen = pnext - next;

                // number of chars in the next line to the left of the caret:
                var right = (nlen <= left) ? 0 : (nlen - left - 1);

                var change = (next - insertionS) + nlen - right;
                setRange(text, insertionS + change, insertionE + change);

            break;

            default:
                insertAtCaret(text,
                   (ch == "Enter" ? (window.opera ? '\r\n' : '\n') : ch));
            }
        }
    }

    // This function retrieves the position (in chars, relative to
    // the start of the text) of the edit cursor (caret), or, if
    // text is selected in the TEXTAREA, the start and end positions
    // of the selection.
    //
    function getCaretPositions(ctrl)
    {
        var CaretPosS = -1, CaretPosE = 0;

        // Mozilla way:
        if(ctrl.selectionStart || (ctrl.selectionStart == '0'))
        {
            CaretPosS = ctrl.selectionStart;
            CaretPosE = ctrl.selectionEnd;

            insertionS = CaretPosS == -1 ? CaretPosE : CaretPosS;
            insertionE = CaretPosE;
        }
        // IE way:
        else if(document.selection && ctrl.createTextRange)
        {
            var start = end = 0;
            try
            {
                start =
                    Math.abs(
                    document.selection.createRange().moveStart("character",
                    -10000000)); // start

                if (start > 0)
                {
                    try
                    {
                        var endReal =
                            Math.abs(
                            ctrl.createTextRange().moveEnd("character",
                            -10000000));

                        var r = document.body.createTextRange();
                        r.moveToElementText(ctrl);
                        var sTest =
                            Math.abs(r.moveStart("character", -10000000));
                        var eTest =
                            Math.abs(r.moveEnd("character", -10000000));

                        if((ctrl.tagName.toLowerCase() != 'input') &&
                            (eTest - endReal == sTest))
                            start -= sTest;
                    }
                    catch(err) {}
                }
            }
            catch (e) {}

            try
            {
                end =
                    Math.abs(
                    document.selection.createRange().moveEnd("character",
                    -10000000)); // end
                if(end > 0)
                {
                    try
                    {
                        var endReal =
                            Math.abs(
                            ctrl.createTextRange().moveEnd("character",
                            -10000000));

                        var r = document.body.createTextRange();
                        r.moveToElementText(ctrl);
                        var sTest =
                            Math.abs(r.moveStart("character", -10000000));
                        var eTest =
                            Math.abs(r.moveEnd("character", -10000000));

                        if ((ctrl.tagName.toLowerCase() != 'input') &&
                            (eTest - endReal == sTest))
                             end -= sTest;
                    }
                    catch(err) {}
                }
            }
            catch (e) {}

            insertionS = start;
            insertionE = end
        }
    }

    function setRange(ctrl, start, end)
    {
        if(ctrl.setSelectionRange) // Standard way (Mozilla, Opera, ...)
        {
            ctrl.setSelectionRange(start, end);
        }
        else // Microsoft Internet Explorer
        {
            var range;

            try
            {
                range = ctrl.createTextRange();
            }
            catch(e)
            {
                try
                {
                    range = document.body.createTextRange();
                    range.moveToElementText(ctrl);
                }
                catch(e)
                {
                    range = null;
                }
            }

            if(!range) return;

            range.collapse(true);
            range.moveStart("character", start);
            range.moveEnd("character", end - start);
            range.select();
        }

        insertionS = start;
        insertionE = end;
    }

    function deleteSelection(ctrl)
    {
        if(insertionS == insertionE) return;

        var tmp =
            (document.selection &&
            !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
        ctrl.value =
            tmp.substring(0, insertionS) + tmp.substring(insertionE,
            tmp.length);

        setRange(ctrl, insertionS, insertionS);
    }

    function deleteAtCaret(ctrl)
    {
        if(insertionS != insertionE)
        {
            deleteSelection(ctrl);
            return;
        }

        if(insertionS == insertionE)
        insertionS = insertionS - 1;

        var tmp =
            (document.selection &&
            !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
        ctrl.value =
            tmp.substring(0,
            insertionS) + tmp.substring(insertionE, tmp.length);

        setRange(ctrl, insertionS, insertionS);
    }

    // This function inserts text at the caret position:
    //
    function insertAtCaret(ctrl, val)
    {
        if(insertionS != insertionE) deleteSelection(ctrl);

        if(isgecko && document.createEvent && !window.opera)
        {
            var e = document.createEvent("KeyboardEvent");

            if(e.initKeyEvent && ctrl.dispatchEvent)
            {
                e.initKeyEvent("keypress", // in DOMString typeArg,
                    false,                 // in boolean canBubbleArg,
                    true,                  // in boolean cancelableArg,
                    null,                  // in nsIDOMAbstractView viewArg,
                    false,                 // in boolean ctrlKeyArg,
                    false,                 // in boolean altKeyArg,
                    false,                 // in boolean shiftKeyArg,
                    false,                 // in boolean metaKeyArg,
                    null,                  // key code;
                    val.charCodeAt(0));    // char code.

                ctrl.dispatchEvent(e);
            }
        }
        else
        {
            var tmp =
                (document.selection &&
                !window.opera) ? ctrl.value.replace(/\r/g,"") : ctrl.value;
            ctrl.value =
                tmp.substring(0,
                insertionS) + val + tmp.substring(insertionS,
                tmp.length);
        }

        setRange(ctrl, insertionS + val.length, insertionS + val.length);
    }
    //--></SCRIPT>

</HEAD>

<BODY>

    ...

    <-- Don't forget this 'onclick' and 'onkeyup': -->
    <TEXTAREA onkeyup="getCaretPositions(this);"
        onclick="getCaretPositions(this);"

        id="textfield" rows="12" cols="50">
    </TEXTAREA>

    ...

</BODY>

Note that the up and down arrows on a virtual keyboard work only on standards-compliant browsers! Take this into account when creating touch screen applications. You can test the above script by running the 2-edit-full.html file found in the vkboard folder of the attached archive. Basic callback is demonstrated in 1-edit-simple.html.

Tips and Tricks

Script flow is quite straightforward, so I hope it won't be hard to dive into it. Here are a couple of words on a few tricky places.

  • Event set-up. We need to handle both Microsoft Internet Explorer and W3C DOM event models:

    _setup_event: function(elem, eventType, handler)
    {
        return (elem.attachEvent ? // Microsoft Internet Explorer way
    
            elem.attachEvent("on" + eventType, handler) :
            ((elem.addEventListener) ? // W3C way
    
            elem.addEventListener(eventType, handler, false) : null));
    }
  • Key container set-up. Each key consists of the "outer" DIV -- where we set the top, left, width and height parameters only -- and the "inner" DIV, which accepts padding, border color and other parameters. We use such a complex construction to circumvent the box model problem of modern browsers. Note that there is the JavaScript solution. If you wish to avoid the box model problem using CSS, you may wish to see the article by Trenton Moss (see item #6).

    _setup_key: function(parent, id, top, left, width, height,
        text_align, line_height, font_size,
        font_weight, padding_left, padding_right)
    {
        var exists = document.getElementById(id);
    
        // Outer DIV:
    
        var key =
            exists ? exists.parentNode : document.createElement("DIV");
        this._setup_style(key,
            !exists, top, left, width, height, "absolute");
    
        // Inner DIV:
    
        var key_sub = exists ? exists : document.createElement("DIV");
        key.appendChild(key_sub); parent.appendChild(key);
    
        this._setup_style(key_sub,
            !exists, "", "", "", line_height, "relative",
            text_align, line_height, font_size, font_weight,
            padding_left, padding_right);
            key_sub.id = id;
    
        return key_sub;
    }
  • Disabling content selection. This one is very important due to the very high typing speed that some people can achieve and, as a result, the inevitable vkeyboard content selection. It can be used instead of the UNSELECTABLE (Microsoft Internet Explorer) and -moz-user-select (Gecko-based browsers) properties.

    this._setup_event(kb_main, "selectstart",
        function(event)
        {
            return false;
        });
    this._setup_event(kb_main, "mousedown",
        function(event)
        {
            if(event.preventDefault)
                event.preventDefault();
            return false;
        });
    

Code

Virtual Keyboard Features at a Glance

  • A complete JavaScript toolkit suitable for simulating every single aspect of a real keyboard device
  • Compact (42.8 KB - compressed full variant) script that doesn't require any images, for faster download
  • Works and looks the same way on all mainstream browsers, tested on Mozilla Firefox 1.5/2, Opera 7.5/9.22, Safari 3, Microsoft Internet Explorer 6/7
  • Very simple set-up procedure
  • Self-contained, with no external dependencies
  • Customizable font, font size and colors; perfect for skinable environments
  • Several variants are available, including full keyboard, slim full keyboard, numpad and ATM-style numpad
  • Compressed scripts are bundled, i.e. full script: 42.8 KB, slim: 43 KB, numpad: 9.30 KB, atm-numpad: 8.62 KB
  • Ten frequently used keyboard layouts are bundled with the full and slim scripts, 24 more layouts are in the provided layout pack
  • Flexible installation options, i.e. any number of vkeyboards per page, adjustable callbacks
  • Open-source script distributed under the zlib/libpng license, meaning that it can be used free of charge even on commercial sites

Script Requirements

Any browser that is aware of:

  • JavaScript (implementation compliant with ECMAScript Edition 3)
  • W3C DOM Level 1
  • CSS Level 1 (at least, most of the elements)
  • W3C or Microsoft Internet Explorer event model

Links

History

  • April 10th, 2006
    • First version, English layout only
  • April 14th, 2006
    • Code refined
    • Russian, German, French, and Czech layouts added
  • April 22nd, 2006
    • Second code revision, much more compact and robust code
    • Spanish, Italian, and Greek layouts added
  • May 14th, 2006
    • Third code revision, even more robust and error-proof code
    • Canadian (multilingual standard) and Hebrew layouts added
    • "AltGr" key and appropriate layout variants added
    • German, French, Spanish, and Greek layouts fixed
    • "Caps Lock" + "Shift" case switching fixed
    • Language menu positioning issue fixed
    • Moved from manual to on-focus switching in the "test2" example
  • May 16th, 2006
    • All "box model"-related issues solved
  • June 12th, 2006
    • Most layouts revised and fixed
    • Minor language menu issue fixed
    • Pilot implementation of the dead keys subsystem
  • August 28th, 2006
    • Keyboard now pops over the page rather than embedding into it
    • Keyboard font size can now be specified, keyboard scales uniformly with font size
    • Keyboard colors can now be customized
    • Shift and AltGr keys now deactivate after alphanumeric key has been pressed
    • Fixed an obscure language menu bug sometimes occurring in Microsoft Internet Explorer
    • Compressed version of script is now bundled in the archive
    • Minor code changes and cleanups, too numerous to be listed
  • September 14th, 2006
  • October 4th, 2006
    • Numpad-only -- 2 variants -- scripts are now bundled in the archive
    • [full keyboard] Fixed problem with an improper cursor positioning when used with Hebrew in edit_simple sample
    • [full keyboard] Many corrections to Greek layout; letters with acute, umlaut and Dialytika Tonos accents are now available
    • [full keyboard] Added optional switch to ShowVKeyboard, that allows you not to create the numpad
  • October 16th, 2006
    • [full keyboard] Dead-keys subsystem reworked
    • [all variants] Slightly more compact and clean code
  • October 26th, 2006
    • Popup-tuned variant of the vkeyboard created
    • [all variants] Key parts of the script were rewritten, resulting in smaller and faster code
    • [all variants] Fixed the annoying issue with key text selection when "typing" fast, due to eventual drag 'n' drop
    • [all variants] Got rid of all browser detection code
    • [full keyboard] Advanced callback function was rewritten; works OK with Microsoft Internet Explorer 6, Firefox 1.5, Opera 9 and Netscape 8.1
  • November 16th, 2006
    • [full keyboard] "test_fly_anonym" sample added
    • [full/popup keyboard] 'new shekel' symbol added to the Hebrew layout
    • [full/popup keyboard] Language names in the language menu were rewritten in the languages they represent
  • December 22nd, 2006
    • [full/popup keyboard] Fixed an issue with the transparent background of the language drop-down menu
    • [full/popup keyboard] Fixed an issue with the language menu not receiving the same font color as the rest of the keyboard
    • [full/popup keyboard] Fixed an issue with the Russian language designator sometimes shown with odd characters
    • [numpad/ATM-numpad] Optimizations and code size reductions
    • [all variants] "Background color" separated into "keyboard base color" and "keys' background color"
    • [all variants] Keyboard font can now be specified
    • [all variants] Further preparations to the next milestone release
  • January 12th, 2007
    • *** Version 2 - interface is incompatible with previous releases ***
    • [all variants] Code refactored and rewritten in an object-oriented fashion, new API, no global variables exposed
    • [all variants] Massive code optimizations, especially in _refresh_layout and _construct (former ShowVKeyboard) methods
    • [all variants] Usage of the eval function was reduced to the absolute minimum: 6 calls in the beginning of _refresh_layout
    • [all variants] Callback function and all visual parameters now can be adjusted at run-time
    • [all variants] Callback function is now stored by reference, not by name
    • [all variants] Callback function now can accept a second parameter
    • [all variants] As a result of refactoring, script now runs up to 30% faster
    • [all variants] Font size can now be specified with any absolute or relative CSS length unit (experimental plug-in; see note)
    • [all variants] Fixed bug when script calculated erroneous padding when changing font size
    • [all variants] Resolved issue with Microsoft Internet Explorer /Mozilla font scaling
    • [full/popup keyboard] Numpad can now be shown/hidden at run-time
    • [full/popup keyboard] Fixed issues with "Enter" key misalignment and glitches
    • [full/popup keyboard] Fixed bug with numpad keys' misalignment when changing font size
    • [full/popup keyboard] Fixed bug when AltGr didn't shadow when Shift was pressed and _alt_gr_shift array was not defined
    • [full keyboard] test_any_css , test_change and test_scale samples added
    • [full keyboard] All samples renamed to form a more consistent test suite
    • [full keyboard] More comments written in all sample installations
    • [popup keyboard] Popup variant is no longer bound to 2 rows per 5 languages each - adaptive configuration
    • changelog.txt (version history) and how-to-compress.txt (obfuscation procedure) files are now shipped with the archive
  • February 6th, 2007
    • [full/popup keyboard] Fixed bug with dead_color parameter not affecting dead keys' color
    • [full/popup keyboard] Breve, DotAbove, DoubleAcute, Macron, and Ogonek diacritic arrays added
    • [full/popup keyboard] Acute, Grave, and Tilde diacritic arrays corrected
    • [ATM-numpad] Fixed erroneous sample
    • [all variants] Better advanced callback function:
      • Fixed bug with improper deletion of multiple characters at once (Mozilla, Opera, Netscape, Microsoft Internet Explorer)
      • Fixed bug with improper character replacements (Microsoft Internet Explorer)
      • Plays better with INPUT fields of type="text"
  • February 14th, 2007
    • *** Version 2.2 - interface is incompatible with previous releases ***
    • [full keyboard] First release of the layout pack; see list
    • [full keyboard] Fixed bugs in advanced callback function that were introduced in the previous release
    • [full keyboard] Macron diacritic array moved to a separate file
    • [full/popup keyboard] New parameter added - background color for the inactive keys
    • [full/popup keyboard] Built-in Czech layout now matches one in IBM layout database, older variant moved to the layout pack
    • [full/popup keyboard] Built-in Hebrew layout now matches one in IBM layout database for normal/casp/shift parts
  • March 26th, 2007:
    • [all variants] Major code cleanup (no evals now!), much cleaner and, generally, faster code
    • [all variants] "keys" can now flash on click, flash colors are customizable
    • [all variants] Virtual keyboard can now either embed into the page or float over the content
    • [all variants] More parameters added (5 to constructor, 4 to SetParameters method) to control the above two features
    • [all variants] Fixed some key text selection issues
    • [full keyboard] Advanced callback function is now (almost) perfect
    • [full/popup keyboard] Reserved keys fired symbol when pressed, fixed
    • [layout pack] Arabic (470) layout added
    • [layout pack] All layout files reformatted to be more readable
  • July 12th, 2007:
    • [full keyboard] Translator script added, scripts starting from 4th renamed to encompass this new test case
    • [full keyboard] 3rd input field in '5-test-fly' and '6-test-fly-anonym' was replaced by a TEXTAREA
    • [full keyboard] Advanced callback function fixed once again (we'll get it right eventually...)
    • [popup keyboard] "popup" variant renamed to "slim", popup sample was dropped due to its poor usability
    • [full/slim keyboard] You can now specify the index of the start-up layout
    • [full/slim keyboard] You can change the layout programmatically with SetParameters function
    • [all variants] You can now specify if there will be a 1-pixel gap between the keys
    • [all variants] Advanced callback function is now the base for all test cases
    • [all variants] Most test cases now make use of backfocus function to address the 'lost focus' annoyance
    • [all variants] Obfuscation procedure was significantly simplified
    • [all variants] Fixed the use of static kbArray variable (doesn't affect you if you're using 1 vkeyboard per page)
  • July 18th, 2007
    • [all variants] Minor optimizations
    • [layout pack] Dutch, Icelandic and Portuguese layouts added
  • July 24th, 2007
    • *** Version 2.6 - interface is incompatible with previous releases ***
    • [full/slim keyboard] Arrow keys can now be shown on vkeyboard creation
    • [full/slim keyboard] Advanced callback function modified to accommodate arrow keys
    • [all variants] Obfuscation procedure was simplified even more
  • March 26th, 2008
    • [full/slim keyboard] Fixed bug with vkeyboard collapse when using arrow keys and the numpad
    • [all variants] Fixed issue with eventual missing of clicks (= VKeyboard is now perfectly usable by touch-screen users)
    • [all variants] Cleared the copyright issue with a DocumentSelection library by Ilya Lebedev
    • [all variants] Obfuscation procedure was simplified once again

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here