Partilhar via


IE8 Developer Tools: Why can’t I cancel the F12 key?

Hi Everyone!

 

Bac again with an interesting issue in IE8.  As everyone is aware, F12 is a keyboard shortcut to bring up the Developer Tools in IE8.  Due to application compatibility reasons, one may want to cancel this default behavior to handle his/her own actions.  The following code snippet has been floating around the Internet showing how to cancel this key:

function CancelF12()

{

     var KeyAscii = window.event.keyCode;

     if (KeyAscii == 123)

    {    

        window.event.returnValue = false;

        window.event.keyCode = 0;

    }

}

The above technique works great most of the time.  Last week, we came across a situation where the above code breaks.  Conditions contributing to the failure include:

1. ShowModalDialog() is involved

2. The code must be run in the Internet zone (or Restricted sites zone)

Note the security zone is important here.  The problem goes away if the code is executed in either the Local Intranet or Trusted sites zones.

Repro Steps:

1) Construct test.html and modal.html pages with the following code:

Test.html:

<html>

<body>

<input type="button" onclick="window.showModalDialog('modal.html');" value= "click me">

</body>

</html>

Modal.html:

<html>

<head>

<title>Modal</title>

</head>

<script>

function CancelF12()

{

                       var KeyAscii = window.event.keyCode;

                       if (KeyAscii == 123)

                       {

                                      window.event.returnValue = false;

                                       window.event.keyCode = 0;

                       }

}

</script>

<body onkeydown="CancelF12()">Try pressing F12</body>

</html>

2) Click the button and press F12

After spending some time debugging, the problem turns out to be this setting in the Internet security zone which causes the code to break:  Allow websites to open windows without address or status bars.  It’s disabled by default in the Internet zone, while enabled in both the Local Intranet and Trusted sites zones.  We don’t recommend customers to change the security setting in the Internet zone as that may compromise the security feature set for the entire Internet URL’s.  A better option would be to add that particular URL to the Trusted Sites zone:

 

clip_image001

 

That’s all for now.  Thanks for reading this blog post!

 

Regards,

The IE Support Team

Comments

  • Anonymous
    October 06, 2009
    Windows Registry Editor Version 5.00[HKEY_CURRENT_USERSoftwarePoliciesMicrosoftInternet ExplorerIEDevTools]"Disabled"=dword:00000001
  • Anonymous
    December 04, 2009
    And if my app needs to use the F12 key? The "CancelF12" function will block my app functionality?
  • Anonymous
    July 09, 2010
    @Rogerio A. Valente : nope. But if you can't understand the simple code posted above, you shouldn't be coding applications.
  • Anonymous
    March 15, 2011
    I run into this very same situation.    The odd part is that only the F12 cannot be captured this way.   You can capture F1-F11.    F12 brings up the Developer Tools window, and it will supercede any attempt to capture the F12 key, when running in the codes as described above.I think Microsoft needs to stop messing around with the Function Keys.  The F12 key was a free to use key prior to IE8, and we have many screens using it to do things.   Now, we have to re-code many of the screens.