[Javascript] shortcuts

Steve Clay sclay at ufl.edu
Wed May 24 10:28:59 CDT 2006


>> On Wed, May 24, 2006 at 03:18:25PM +0200, Michael Borchers wrote:
>> <a href="<?php echo CONTACTS_FILENAME; ?>?select=1"
>>   target="_top" 
>>   accesskey="c">link text</a>

> the links appear somewhere else in a dropdown where i can't
> add a accesskey. that's why i considered to use hidden fields.

The big problems with the original solution is that it breaks tab
navigation through the page. Another idea would be to not use accesskeys or
markup at all. 

The following should "listen" for Shift+(some key), which is probably a
better idea than listening for alt/ctrl combinations (more likely to be
built-in browser behaviors.) Note you'll have to set shortcutsActive to
false while the user is in a text entry field, or the user will be whisked
away when they start a sentence with "C".

// untested!
var shortcutsActive = true;
document.onkeypress = function(e) {
  if (!e) var e = window.event;
  if (shortcutsActive && e.shiftKey) { // shift +
    switch (e.keyCode) {
      case 67:                         // 'c'
        window.location = 'urlHere';
      break;
    }
  }
  // also available:
  // e.ctrlKey
  // e.altKey (unsupported in Opera)
}

references:
http://www.quirksmode.org/js/events_compinfo.html#keys
http://www.quirksmode.org/js/events_compinfo.html#prop
http://www.quirksmode.org/dom/w3c_events.html#keyprops
http://www.quirksmode.org/js/events/keys.html

Steve
-- 
http://mrclay.org/




More information about the Javascript mailing list