browser keypress selects page key
authorMischa POSLAWSKY <perl@shiar.org>
Sat, 21 Mar 2009 02:41:12 +0000 (02:41 +0000)
committerMischa POSLAWSKY <perl@shiar.org>
Wed, 22 Apr 2009 00:01:21 +0000 (00:01 +0000)
Javascript onkeypress handler which marks and clicks on a matching key
if it is pressed in the browser.

Increases Vim page size by 11kB (approx 10%) because each key requires
a class name for unambiguous character identification.  With gzip
compression the difference is just 1422 bytes (though still 10%),
so assume this is (or at least will be) a worthwhile trade-off.

Shiar_Sheet/Keyboard.pm
keys.js
nethack.plp
vim.plp
vimperator.plp

index 58d7da2905978fd6f0903d0c941f397acae603ad..9456b7d1f212b5c47f787125fead8a62bc557e50 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 no  warnings 'uninitialized';  # save some useless checks for more legible code
 use Carp;
 
 no  warnings 'uninitialized';  # save some useless checks for more legible code
 use Carp;
 
-our $VERSION = '1.02';
+our $VERSION = '1.03';
 
 my @casedesc = (undef, qw/shift ctrl meta/, 'shift meta');
 my @rowdesc = qw(numeric top home bottom);
 
 my @casedesc = (undef, qw/shift ctrl meta/, 'shift meta');
 my @rowdesc = qw(numeric top home bottom);
@@ -97,6 +97,7 @@ sub print_key {
        $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
                if $flags =~ s/ ?\blink(\S*)//;
        my $keyhint = defined($mnem) && qq{ title="$mnem"};
        $onclick .= sprintf(q{ onclick="document.location='%s'"}, $1)
                if $flags =~ s/ ?\blink(\S*)//;
        my $keyhint = defined($mnem) && qq{ title="$mnem"};
+       $flags .= ' chr'.ord(substr $key, -1) if $key ne '^0';
 
        print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
        print ' ', $desc if defined $desc;
 
        print qq{\t\t<li class="$flags"$onclick><b$keyhint>$keytxt</b>};
        print ' ', $desc if defined $desc;
diff --git a/keys.js b/keys.js
index d891b528d2bd9362ee6be15baeba2f92928bb4f1..5dbfb104f3f34cfa847c4c79264763ec6eafbc6c 100644 (file)
--- a/keys.js
+++ b/keys.js
@@ -23,3 +23,30 @@ function setmode(classname) {
        }
 }
 
        }
 }
 
+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;
+       for (var i = 0; i < keylabels.length; i++) {
+               var key = keylabels[i].parentNode;
+               var keychar = key.className.match(/ chr(\d+)$/);
+               if (!keychar) continue; // not enterable
+               keychar = keychar[1];
+               if (keychar != input) continue; // different key
+               var keymod = key.parentNode.className;
+               if ((keymod.search(/\bctrl\b/) != -1) != e.ctrlKey) continue; // modifier mismatch
+               if ((keymod.search(/\bmeta\b/) != -1) != e.altKey) continue;
+               var row = key.parentNode.parentNode;
+               var shown = row.style.display != 'none';
+               if (!shown) continue; // foreign mode
+               if (keyfocus) keyfocus.style.outline = '';
+               key.style.outline = '1px solid red';
+               keyfocus = key;
+               if (key.onclick) key.onclick();
+               return false;
+       }
+}
index dc8e616d826a36897e22b19c20851e53abbe818f..b6f8a4a4df8f529089bfd5f8fb5c81a27e2da7f9 100644 (file)
@@ -44,7 +44,7 @@ $header{content_type} = "text/html; charset=$sign{charset}";
 
 <li class="row">
        <ul class="keys omni">
 
 <li class="row">
        <ul class="keys omni">
-       <li class="me" onclick="setmode()"><b>Esc</b>
+       <li class="me chr27" onclick="setmode()"><b>Esc</b>
                <!-- static reset button, even though it's not (officially) in the game -->
        </ul>
 </li>
                <!-- static reset button, even though it's not (officially) in the game -->
        </ul>
 </li>
diff --git a/vim.plp b/vim.plp
index b8a1f5c5083b69d08e9007b9d43d052443a92f35..af7407400cd15e283f421b865a5b454437c96cda 100644 (file)
--- a/vim.plp
+++ b/vim.plp
@@ -44,7 +44,7 @@ $header{content_type} = "text/html; charset=$sign{charset}";
 
 <li class="row">
        <ul class="keys omni">
 
 <li class="row">
        <ul class="keys omni">
-       <li class="mo" onclick="setmode()"><b>Esc</b> normal mode
+       <li class="mo chr27" onclick="setmode()"><b>Esc</b> normal mode
                <!-- not as static anymore, but never bothered; just see ^[ -->
        </ul>
 </li>
                <!-- not as static anymore, but never bothered; just see ^[ -->
        </ul>
 </li>
index cdde680ad89c03ab1023af3af892578beb2298fc..19298d1b4042e9d8d6995f877c6412768ab69302 100644 (file)
@@ -44,7 +44,7 @@ $header{content_type} = "text/html; charset=$sign{charset}";
 
 <li class="row">
        <ul class="keys omni">
 
 <li class="row">
        <ul class="keys omni">
-       <li class="mv" onclick="setmode()"><b>Esc</b> normal mode
+       <li class="mv chr27" onclick="setmode()"><b>Esc</b> normal mode
                <!-- not as static anymore, but never bothered; just see ^[ -->
        </ul>
 </li>
                <!-- not as static anymore, but never bothered; just see ^[ -->
        </ul>
 </li>