Microsoft keyboard layout interpreter

By GreyWyvern Last update Oct 13, 2009 — Installed 796 times.

There are 4 previous versions of this script.

// ==UserScript==
// @name        Microsoft keyboard layout interpreter
// @copyright   2009+, GreyWyvern (http://www.greywyvern.com/code/javascript/keyboard)
// @namespace   http://www.greywyvern.com/ 
// @version     1.0
// @description Generate a Virtual Keyboard Interface layout from a keyboard on the MSDN.  Javascript code generated by this script may still need to be manually modified before inclusion.
// @license     BSDL; http://www.opensource.org/licenses/bsd-license.php
// @include     http://www.microsoft.com/globaldev/keyboards/kbd*
// @include     http://www.microsoft.com/resources/msdn/goglobal/keyboards/kbd*
// ==/UserScript==

/* ********************************************************************
 **********************************************************************
 * Microsoft keyboard layout interpreter - v1.0
 *   Copyright (c) 2009 - GreyWyvern
 *
 *  - Licenced for free distribution under the BSDL
 *          http://www.opensource.org/licenses/bsd-license.php
 *
 * Reinterprets keyboard layouts found at the Microsoft Developers
 * Network into javascript code suitable for inclusion into the
 * Virtual Keyboard Interface user script:
 *   http://userscripts.org/scripts/show/10974
 *
 * This script is not a replacement for manual proofreading, but gets
 * a lot of the grunt work done for layouts available from Microsoft.
 * Not all keyboard layouts are available from the MSDN.
 *
 * Version 1.0 - October 5, 2009
 *
 */
window.addEventListener('load', function() {
  var keyboard = [];

  function getKeys(rows, index) {
    for (var x = 0; x < rows.length; x++) {
      if (!keyboard[x]) keyboard[x] = [];
      for (var y = 0, keys = rows[x].getElementsByTagName('img'); y < keys.length; y++) {
        if (!keyboard[x][y]) keyboard[x][y] = [];
        var code = keys[y].src.replace(/^.+\/([^\/]+?)[WK]?\.jpg$/gi, "$1").replace(/_/g, "");
        if (code.match(/^[\da-f]+$/i)) {
          keyboard[x][y][index] = "";
          for (var z = 0; z < code.length; z += 4) {
            var character = code.substr(z, 4), decval = parseInt(character, 16);
            keyboard[x][y][index] += (decval >= 32 && decval <= 126) ? String.fromCharCode(decval) : ((character == "0000") ? ((index) ? "" : " ") : "\\u" + character);
          }
        } else {
          switch (code) {
            case "Tab":          keyboard[x][y][index] = (index <= 1) ? "Tab" : ""; break;
            case "BackSp":       keyboard[x][y][index] = (index <= 1) ? "Bksp" : ""; break;
            case "Enter":
            case "EnterX":       keyboard[x][y][index] = (index <= 1) ? "Enter" : ""; break;
            case "CapsLoc":
            case "CapsLock":     keyboard[x][y][index] = (index <= 1) ? "Caps" : ""; break;
            case "Shift":
            case "Shift2":
            case "NarrowShift":
            case "NarrowShift2": keyboard[x][y][index] = (index <= 1) ? "Shift" : ""; break;
            case "Alt":
            case "Alt2":         keyboard[x][y][index] = (index <= 1) ? "Alt" : ""; break;
            case "AltGr":
            case "AltGr2":       keyboard[x][y][index] = (index <= 1) ? "AltGr" : ""; break;
            case "Key4":         keyboard[x][y][index] = " "; break;
            case "None":
            case "Ctrl":
            case "Control": break;
            default: alert(code);
          }
        }
      }
    }
  }

  getKeys(document.getElementById('normalstate').getElementsByTagName('tr'), 0);
  getKeys(document.getElementById('shiftstate').getElementsByTagName('tr'), 1);

  var altstate = document.getElementById('altgrstate') || document.getElementById('altstate');
  if (altstate) {
    getKeys(altstate.getElementsByTagName('tr'), 2);
    var altstate = document.getElementById('shiftaltgrstate') || document.getElementById('shiftaltstate');
    getKeys(document.getElementById('shiftaltgrstate').getElementsByTagName('tr'), 3);
  }

  for (var x = 0; x < keyboard.length; x++) {
    for (var y = keyboard[x].length - 1; y >= 0; y--) {
      for (var z = keyboard[x][y].length - 1; z >= 0; z--)
        if (!keyboard[x][y][z]) { keyboard[x][y].pop(); } else break;
      if (keyboard[x][y].length) {
        keyboard[x][y] = '"' + keyboard[x][y].join('", "') + '"';
      } else keyboard[x].splice(y, 1);
    } keyboard[x] = '[' + keyboard[x].join('], [') + ']';
  } keyboard = '  [' + keyboard.join('],\n  [') + ']';
  keyboard = keyboard.replace(/\[" "\]/g, '[""]').replace(/"""/g, "'\"'").replace(/"\\"/g, '"\\\\"');

  var title = document.getElementsByTagName('title')[0].firstChild.nodeValue.replace(/ Keyboard Layout/gi, "");
  var prop = (title.match(/^[a-z]+$/gi)) ? "." + title : '["' + title + '"]';
  keyboard = "this.VKI_layout" + prop + " = [ // " + title + " Standard Keyboard\n" + keyboard + "\n];";

  var pre = document.createElement('pre');
  pre.appendChild(document.createTextNode(keyboard));
  pre.style.backgroundColor = "#eeeeee";
  pre.style.border = "2px solid #cccccc";
  pre.style.padding = "10px";
  pre.style.overflow = "auto";
  document.body.insertBefore(pre, document.body.firstChild);
}, false);