[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 6657
  • Last Modified:

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.
0
paku
Asked:
paku
  • 3
  • 3
1 Solution
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now