+window.addEventListener('hashchange', function(e) {
+ setmode(location.hash.slice(1), true);
+});
+if (location.hash) {
+ setmode(location.hash.slice(1), true);
+}
+
+var keyfocus = undefined;
+document.onkeypress = function(e) {
+ var keylabels = document.getElementById('rows').getElementsByTagName('B');
+ var keys = {};
+ for (var i = 0; i < keylabels.length; i++) {
+ keys[keylabels[i].innerHTML] = keylabels[i].parentNode;
+ }
+ var input = e.charCode || e.keyCode;
+
+ // find key element matching input
+ for (var i = 0; i < keylabels.length; i++) {
+ var key = keylabels[i].parentNode;
+ if (!key.onclick) continue; // link
+ var keychar = key.className.match(/ chr(\d+)$/);
+ if (!keychar) continue; // has code
+ keychar = keychar[1];
+ if (keychar != input) continue; // matches code
+
+ // match mode (visibility)
+ var row = key.parentNode;
+ var shown = row.style.display != 'none';
+ if (!shown) continue;
+
+ // match key modifiers
+ var keymod = row.className;
+ if ((keymod.search(/\bctrl\b/) != -1) != e.ctrlKey) continue;
+ if ((keymod.search(/\bmeta\b/) != -1) != e.altKey) continue;
+
+ // select
+ if (keyfocus) keyfocus.style.outline = '';
+ key.style.outline = '1px solid red';
+ keyfocus = key;
+ if (key.onclick) key.onclick();
+ return false;
+ }
+}