How to disable keyboard F5 button ?

Hi

I want to disable the F5 , Ctrl+R or Ctrl+ N, Backspace etc buttons. Actually i am developing Online test module and the purpose is the end user should not be able to refresh or open a new window in between the test. Here is the code which i have got from net but its not working can anybody help me ...

<html>
<head>
<script type="text/JavaScript">

       function showDown(evt) {
      
        evt = (evt) ? evt : ((event) ? event : null);
        if (evt) {
            alert(evt.keyCode);
            if (window.event.keyCode == 8 && (window.event.srcElement.type != "text" && window.event.srcElement.type != "textarea" && window.event.srcElement.type != "password"))
                  {
                // When backspace is pressed but not in form element
                        alert("When backspace is pressed but not in form element");
                cancelKey(evt);
            }
            else if (window.event.keyCode == 20) {
                // When F5 is pressed
                        alert("When F5 is pressed")
                cancelKey(evt);
            }
            else if (window.event.ctrlKey && (window.event.keyCode == 78 || window.event.keyCode == 82)) {
                // When ctrl is pressed with R or N
                        alert("When ctrl is pressed with R or N");
                cancelKey(evt);
            }
        }
    }

    function cancelKey(evt) {
      alert("Inside cancelKey(evt)");
        if (evt.preventDefault) {
            evt.preventDefault();
            return false;
        }
        else {
            evt.keyCode = 0;
            evt.returnValue = false;
        }
    }


</script>

</head>

<body onKeyPress = "showDown(winodw.event)">
<FORM ACTION="">
</FORM>
</body>
</html>


Prakash.
pakuAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GwynforWebCommented:
This not going to happen, not all key presses can be detected. It is part of the security idea of browsers and the internet.
0
sajuksCommented:
Try changing <body onKeyPress = "showDown(winodw.event)">
to
<body onkeyup = "showDown(window.event)">

also you'd a spelling mistake its window and not winodw
0
sajuksCommented:
agree with gfw that this is going to fail in other browsers like netscape,opera
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

pakuAuthor Commented:
Is there any other way to achive this functionality... GwynforWeb.

0
sajuksCommented:
The client browser wont allow you to take control of the browser thru javascript coding.
Check http://tech.irt.org/articles/js195/ for key board trapping events.
0
GwynforWebCommented:
paku, I gather you want to stop reresh. I do know how to do this, but could you tell me why you need to do this?  (going to bed in 5mins)
0
GwynforWebCommented:
ok I have read the question again I see. I was wrong F5 is one that can be detected, you can get at F5 this way  


<head>
<script language="JavaScript1.2">
function keyPressed(evt) {
   var F5 = 116;
   if(document.layers) {
             if (evt.which == F5) {
              return false;
           }
   }else if(document.all){
          if (event.keyCode == F5) {
               event.keyCode = 0;
                window.event.returnValue = false;
             }
   }
}

if(document.layers){
     document.captureEvents (Event.KEYDOWN);
}
document.onkeydown=keyPressed;

</script>
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.