views:

75

answers:

1

I would like to display the user selected help file when pressing F1. This should work on every browser where I test my application. How can I stop the default help file from being displayed?

+2  A: 

AFAIK, the default action of the F1 key can be changed in any browser except for IE. The Microsoft teams are usually sticklers for maintaining a consistent user experience across their applications and that's why F1 opens help regardless of returning false. That being said, there's a workaround in the form of the window.onhelp event.

// Internet Explorer
if ("onhelp" in window)
    window.onhelp = function () { 
        showMyHelpInsteadOfTheUsualDefaultHelpWindow(true); 
        return false;
    }
// Others
else {
    document.onkeydown = function(evt) {
        cancelKeypress = (evt.keyCode == 112);
        if (cancelKeypress) {  // F1 was pressed
            showMyHelpInsteadOfTheUsualDefaultHelpWindow(true);
            return false;
        }
    }

    // Additional step required for Opera
    document.onkeypress = function(evt) {
        if (cancelKeypress) 
            return false;
    }
}

"Others" step was adapted from a deleted answer, which was adapted from another answer which, in turn, was adapted from another answer.

Andy E
Oh no, they try to prevent mangling the normal user experience (which is not a bad thing, IMHO), but they provide another (easier) function to be able to do exactly that?
Marcel Korpel
@Marcel: I worded that wrong. The *onhelp* event is intentionally provided to replace the default help, but it's also intentional that the F1 key cannot be cancelled. Weird, I know.
Andy E