8

I'm trying to add keyboard shortcuts on my website to make fast navigation possible using the keyboard. I'm running into a slight problem, however, with my attempted Alt+X shortcut. The event runs just fine and returns false as it should, but the browser's File menu comes up regardless. I've also tried the preventDefault method, but no change.

The cut-down version of the script is:

document.documentElement.onkeydown = function(e) {
    e = e || window.event;
    switch( e.keyCode || e.which) {
        // some cases here - most notably:
        case 116: // F5 key
            if( activeFrame) {
                activeFrame.contentWindow.location.reload();
                // reloads an iframe if one is active
                return false;
            }
            break;
        // more cases...
        case 88: // X key
            if( e.altKey) {
                // do something
                return false;
            }
    }
}

As noted above, overriding the default action of the F5 key works just fine - the browser reloads the page only if no iframe is active. I don't quite see how to prevent the menu from appearing when Alt+X is pressed.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592

3 Answers3

4

use stopPropagation(e); instead of preventDefault method

function stopPropagation(e)
{
    e = e || event;/* get IE event ( not passed ) */
    e.stopPropagation? e.stopPropagation() : e.cancelBubble = true;
}

Reference link

Another SO question which mentions that preventDefault has issue in IE.

UPDATE

Try using below code as per MSDN Reference

event.returnValue=false;

And some point from Detecting keystrokes

Some general caveats:

  • Generally, Mac is less reliable than Windows, and some keys cannot be detected.
  • Explorer doesn't fire the keypress event for delete, end, enter, escape, function keys, home, insert, pageUp/Down and tab.
  • Onkeypress, Safari gives weird keyCode values in the 63200 range for delete, end, function keys, home and pageUp.Down. The onkeydown and -up values are normal.
  • Alt, Cmd, Ctrl and Shift cannot be detected on Mac, except in Opera. However, you can always use the altKey, ctrlKey, and shiftKey properties.
Community
  • 1
  • 1
Harsh Baid
  • 7,199
  • 5
  • 48
  • 92
  • I think using `ALT` as shortcut key (combination) in website is not an good idea (or not sure but not possible in html) even google docs don't have `ALT` in their shortcuts list. And when we create IE or other browser toolbars then also we have to write extra bit of logic first to `Get event in the toolbar` and then Handle the key down events for `ALT`. – Harsh Baid Oct 30 '11 at 13:38
  • Detecting keystrokes has sample which detects the `alt` key also. – Harsh Baid Oct 30 '11 at 14:01
  • I've just gone and tested the `returnValue=false` thing, but that doesn't seem to stop the menu either. – Niet the Dark Absol Oct 30 '11 at 14:41
3

I actually had a web app working just fine with CTRL shortcut keys, but then decided I'd be clever and use the accesskey attribute, and ran into this exact issue with IE.

The problem with going to CTRL shortcut keys is that many of those are more standard/useful across many applications (eg: cut, copy, paste, select all).

Ctrl+Alt is fairly safe, but requires more work on the user's part.

I tend to just try to stick to ALT shortcuts IE doesn't stubbornly insist on handling.

Demo of CTRL + A/CTRL + F being cancelled successfully: http://jsfiddle.net/egJyT/

This answer seems to imply it isn't possible to disable the menu shortcuts without putting IE into kiosk mode.

Kevin Stricker
  • 17,178
  • 5
  • 45
  • 71
  • Ctrl+Alt is what I'm using at the moment, seems to be doing well. If nobody has a way to actually top the menu, this'll probably be the best answer. – Niet the Dark Absol Oct 30 '11 at 13:28
  • 1
    @RobW I couldn't get *any* of the documented methods to work, which confirms my previous experience. I was addressing the bounty comments. This seems like a semi-official "No, that isn't possible." http://answers.microsoft.com/en-us/ie/forum/ie8-windows_other/how-do-i-disable-ie-assigned-alt-ctrl-keys/3e24800d-06f8-41ac-818a-bc1670bcc47c – Kevin Stricker Oct 30 '11 at 15:40
  • @Kolink I'd choose a combination of `Shift + ctrl` or `Shift + Alt`. Ctrl+alt+... is a commonly used combination for a hotkey, which may interfere with the user's expected behaviour. – Rob W Oct 30 '11 at 15:44
1

Beware that if you manage to successfully prevent the browser from detecting a key combination you may make your page unusable for some users. Many screen readers have reserved almost any key you can think of to control the screen reader and if your page was accessible using a screen reader before you added the shortcut key code, it may be completely un-accessible users needing screen readers after you add it.

Read this article about access keys (a bit old but probably still relevant), and this article about Reserved Keystroke Combinations before you invest too much time on this problem.

Stein G. Strindhaug
  • 5,077
  • 2
  • 28
  • 41
  • Access keys also depends on the browser as this question's issue http://stackoverflow.com/questions/515951/html-attribute-accesskey-not-working-as-it-should – Harsh Baid Oct 30 '11 at 10:59
  • Since the site is a game, it's probably not going to be used by non-graphical browser users very much, if at all, so I don't think affecting accessibility is too much of an issue. Thanks for the heads-up, though, I'll keep it in mind. – Niet the Dark Absol Oct 30 '11 at 13:27