Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: SV: disabling control keys in Netscape


Message #1 by "Robert Nyman" <robert.nyman@c...> on Fri, 6 Dec 2002 17:47:44 +0100
Thanx a million Robert!
That worked just perfect!
And dont u worry!
This is for a security project well within the company's intranet only!
And i can ensure that everyone uses Netscape 6 and above... 
So no probs at all!


And i fixed the F11 key in IE too!
this is the code i used for that:
	if (bIE && event.keyCode == 122)
		{
			event.keyCode = 0;
			alert("Disabled!");
			return false;
		}

(event.returnvalue=false was not working!So i set the keycode to 0 and
just returned false  ;)  )



Thanx once again! Now i can count on u....  :-)



-----Original Message-----
From: Robert Nyman [mailto:robert.nyman@c...]
Sent: Tuesday, December 10, 2002 6:33 PM
To: 'javascript'
Cc: Randolph, Priya
Subject: SV: [javascript] SV: disabling control keys in Netscape


Priya,

After some research, I've found a solution for you!   :-)
This code below will support Netscape 6+ (sorry, no Netscape 4 support).
One thing that I don't seem to be able to prevent, though, is the F11
key in IE, otherwise it just works like a charm.

However, I hope that this code will only be used for a closed
environment, since it's not good 
programming fashion preventing default key shortcuts in the user's
browser.


Code:

<html>
<head>
 <title>Test...</title>

<script language="JavaScript" type="text/javascript">
	var bNS6andUp = (navigator.appName == "Netscape" &&
parseInt(navigator.appVersion, 10) >= 5)? true : false;
	var bIE = (navigator.appVersion.search(/MSIE/) != -1)? true :
false;
	if(bNS6andUp){
		window.addEventListener("keypress", keyPressCheck,
true);
	}
	else if(bIE){
		document.onkeydown = keyPressCheck;
	}
	/*
        KEY CODES/CHAR CODES:
        A: 65 = 97 in Netscape
        B: 66 = 98 in Netscape
        E: 69 = 101 in Netscape
        H: 72 = 104 in Netscape
        I: 73 = 105 in Netscape
        N: 78 = 110 in Netscape
        W: 87 = 119 in Netscape
        8 (NumPad): 104 = 56 in Netscape
     */	
	
	function keyPressCheck(e){
		var oEvent = (bIE)? event : e;
		var intKeyCode = oEvent.keyCode;
		var intCharCode = (bIE)? oEvent.keyCode :
oEvent.charCode;
        		var bCtrlKey = (oEvent.ctrlKey)? true : false;
		if(bNS6andUp && intKeyCode == 122){
			oEvent.preventDefault();
		}
		var oRegExp = (bIE)? new
RegExp(/65|66|69|72|73|78|87|104/) : new
RegExp(/56|97|98|101|104|105|110|119/);
		if(bCtrlKey && oRegExp.test(intCharCode)){
			(bIE)? oEvent.returnValue = false :
oEvent.preventDefault();
        		}
    	}	
</script>

</head>

<body>


</body>
</html>


/Robert



-----Ursprungligt meddelande-----
Från: Robert Nyman [mailto:robert.nyman@c...] 
Skickat: den 6 december 2002 17:48
Till: javascript
Ämne: [javascript] SV: disabling control keys in Netscape


Priya, 

I took the the liberty of rewriting your code (see below). 
BUT, unfortunately I'm experiencing the same problem as you: I can't
prevent the default action in any Netscape browser!!! For instance, this
code blocks Ctrl + N (out of many keyboard shortcuts), and it works in
IE , but it doesn't work in Netscape.

I know that this isn't good programming fashion for the Internet,
preventing default browser action, but for a closed environment it might
be ok.

Am I missing something here, or can't it be done?

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
	<title>Test...</title>
	
<script language="javascript">
	var bIE = (navigator.appVersion.indexOf("MSIE") != -1)? true :
false;
	var bNS4 = (navigator.appName == "Netscape" &&
parseInt(navigator.appVersion, 10) == 4)? true : false;
	var bNS6andUp = (navigator.appName == "Netscape" &&
parseInt(navigator.appVersion, 10) >= 5)? true : false;
	
	if(bNS4) document.captureEvents(Event.KEYDOWN);
	document.onkeydown = keydowncheck;	
	/*	
		KEY CODES:		
		A: 65 = 97 in Netscape 4
		B: 66 = 98 in Netscape 4
		E: 69 = 101 in Netscape 4
		H: 72 = 104 in Netscape 4
		I: 73 = 105 in Netscape 4
		N: 78 = 110 in Netscape 4
		W: 87 = 119 in Netscape 4
		8 (NumPad): 104 = 56 in Netscape 4
	*/  
	function keydowncheck(e){
		var oEvent =  (bIE)? event : e;
		var intKeyCode = (bIE)? oEvent.keyCode : oEvent.which;
		var bCtrlKey = (bNS4)? (oEvent.modifiers &&
Event.CONTROL_MASK)? true : false : (oEvent.ctrlKey)? true : false;
		//alert(bCtrlKey);
		//alert(intKeyCode);
		if((bIE || bNS6andUp) && intKeyCode == 122){
			alert("Disabled");
			if(bIE){
				event.returnValue = false;
				event.cancelBubble = true;
			}
			else if(bNS6andUp){
				e.preventDefault();
				e.stopPropagation();
			}	
			return false;
		}
		var oRegExp = (bNS4)? new
RegExp(/97|98|101|104|105|110|56/) : new
RegExp(/65|66|69|72|73|78|87|104/);
		if(bCtrlKey && oRegExp.test(intKeyCode)){
			alert("Disabled");
			if(bIE){				
				event.returnValue = false;
				event.cancelBubble = true;
			}
			else if(bNS6andUp){
				e.cancelBubble = true;

				e.preventDefault();
				e.stopPropagation();
			}	
			return false;
		}
	} 
</script>	
	
</head>

<body>

</body>
</html>


/Robert


-----Ursprungligt meddelande-----
Från: Randolph, Priya [mailto:priya.randolph@d...] 
Skickat: den 6 december 2002 13:00
Till: javascript
Ämne: [javascript] disabling control keys in Netscape


Hi,
This is Priya.
 
I need to disable the conrol keys like ^N, ^A, ^I etc in Netscape It
works in IE but not in netscape
 
I am able to trap the control key sequence but not DISABLE it :(
 
 
here is the script i use:
 
<script language="javascript">
  var IE=0, NS=0;
// Detect the browser
IE  = (-1 != navigator.appVersion.indexOf("MSIE")) ? 1 : 0;
NS  = ("Netscape" == navigator.appName) ? 1 : 0;
  
  var blnDOM = false, blnIE4 = false, blnNN4 = false; 
 
if (document.getElementById) blnDOM = true;
else if (document.layers) blnNN4 = true;
else if (document.all) blnIE4 = true;
  
  
if (blnNN4 || NS) document.captureEvents(Event.KEYDOWN)
document.onkeydown =keydowncheck
  
   function keydowncheck(e)
   {
   if (NS)
   {
    var NN4key = e.which;
    if (e.which ==  122)
    {
     e.which = 0;
     alert("disabled NS!");
     return false;
    }
    else
   if((NN4key == 78) || (NN4key == 104) || (NN4key == 65) || (NN4key ==
87)
|| (NN4key == 69) || (NN4key == 72) || (NN4key == 66) || (NN4key == 73))
     {
     //e.which=0;
     alert("Disabled Control!");
     //e.returnValue = false;
     e.preventDefault()
     //document.onkeydown=new Function("return false") 
     return false;
     
     }
   
    
  } 
   
   if (blnDOM || blnIE4)
   {
    if (122 == event.keyCode)
    {
     event.keyCode = 0;
     alert("disabled!");
     return false;
    }
 
    else if(event.ctrlKey)
    {
     if((event.keyCode == 78) || (event.keyCode == 104) ||
(event.keyCode ==
65) || (event.keyCode == 87) || (event.keyCode == 69) || (event.keyCode
==
72) || (event.keyCode == 66) || (event.keyCode == 73))
     {
     alert("Disabled!");
      event.returnValue = false;
     }
      
    }
   }
    
   }
 
</script>

 
 
( i have attached the file)
Pls help me with the solution.
 
 
 
 
Regards,
 
Priya Randolph
Digital GlobalSoft Ltd. 
(A subsidiary of Hewlett-Packard Co., USA) 

'Digital Park' , Plot No. 39/40,Konappana Agrahara,
Electronic city, Phase 2,Hosur Road,Bangalore
India 
TEL   : 91-80-8528395/6/7/8 Extn 62112
Email:  <mailto:priya.randolph@d...> priya.randolph@d...
 








  Return to Index