Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>When I got started on this, I didn't intend to rewrite the whole thing. But I did, and you can see it working at <a href="http://jsfiddle.net/slashingweapon/mhvCc/" rel="nofollow">this jsfiddle</a>.</p> <p>The technique is to find all of the target elements and build a list of what key combinations each one maps to. We store these in the array <code>keybindings</code>, and every time someone presses a key we look through the whole list looking for a match. If we find one, we trigger a click event on the element.</p> <p>I used <code>keydown</code> instead of <code>keypress</code> because you actually get the modifier information with it. I also improved the class string parsing, but it could be made a little cleaner I'm sure.</p> <p>Here's the JS code.</p> <pre><code>var keybindings = []; $('[class*="key-"]').each(function (idx, val) { var keyspec = { which: 0, altKey: false, shiftKey: false, ctrlKey: false, element: val }; var keystring = $(val).attr('class').toLowerCase(); var match = /key-\S+/.exec(keystring); var parts = match.toString().split('-'); keyspec.which = parts[ parts.length-1 ].toUpperCase().charCodeAt(0); for (var jdx in parts) { if (parts[jdx] == 'alt') keyspec.altKey = true; else if (parts[jdx] == 'shift') keyspec.shiftKey = true; else if (parts[jdx] == 'ctrl') keyspec.ctrlKey = true; } keybindings.push( keyspec ); }); $(document).keydown( function(evt) { $.each(keybindings, function(idx, oneBind) { if (evt.which == oneBind.which &amp;&amp; evt.altKey == oneBind.altKey &amp;&amp; evt.shiftKey == oneBind.shiftKey &amp;&amp; evt.ctrlKey == oneBind.ctrlKey) $(oneBind.element).click(); }); }); </code></pre> <p>Of course it occurs to me now that there's probably a jquery plug-in for this already and we could just use that.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload