views:

68

answers:

1

I'm trying to suppress IE's default handling of Ctrl+O.

I've got a onKeyDown handler which get's called, but even though I call event.cancelBubble and return false, the default File|Open command still runs.

btw: this is not critical since I can just pick another key but curious if there might be a way around this.

+3  A: 

First, you can't call event.cancelBubble, it's not a method, but a property you can set to true.

To prevent the default action of special keys in IE, you also have to set the IE keycode to 0:

function keydownHandler(e) {
    e = e || window.event;

    if (e.preventDefault)
        e.preventDefault();
    else {
        e.cancelBubble = true;
        e.returnValue = false;
        e.keyCode = 0;
    }
}
Marcel Korpel
has to be registered for `keydown` in IE8. `keyup` doesn't work.
lincolnk
@lincolnk: Correct. You can't suppress default actions during `keyup`, as by then the action already has taken place. Also see http://www.quirksmode.org/dom/events/keys.html#t09
Marcel Korpel
It was the `e.keyCode=0` I was missing. Thanks.
cantabilesoftware