Disable ctrl + n and other ctrl + key combinations in JavaScript    June 17, 2006    44

Few days ago Vijay asked if there’s a way to disable ctrl + n combination (open the new window shortcut. So I set out to create a small Java Script that disables any ctrl + key combination (e.g. ctrl + v, ctrl + c, ctrl + a, etc.).

Script is a bit more complicated than Disable form submit on enter keypress and it should work in both Fire Fox and Internet Explorer.

<script language="JavaScript">
function disableCtrlKeyCombination(e)
{
	//list all CTRL + key combinations you want to disable
	var forbiddenKeys = new Array('a', 'n', 'c', 'x', 'v', 'j');
	var key;
	var isCtrl;

	if(window.event)
	{
		key = window.event.keyCode;     //IE
		if(window.event.ctrlKey)
                        isCtrl = true;
                else
                        isCtrl = false;
	}
	else
	{
		key = e.which;     //firefox
		if(e.ctrlKey)
			isCtrl = true;
		else
			isCtrl = false;
	}

	//if ctrl is pressed check if other key is in forbidenKeys array
	if(isCtrl)
	{
		for(i=0; i<forbiddenkeys .length; i++)
		{
			//case-insensitive comparation
			if(forbiddenKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
			{
				alert('Key combination CTRL + '
					+String.fromCharCode(key)
					+' has been disabled.');
				return false;
			}
		}
	}
	return true;
}
</script>

And you just add this to the field where you’d like to disable keys:

<input type="text" name="mytext" 
	onKeyPress="return disableCtrlKeyCombination(event);"
	onKeyDown="return disableCtrlKeyCombination(event);" />

Although the key combinations are disabled only on one field the same script can be easily modified to disable ctrl + key combinations on whole page.

Also if you are looking for something more sophisticated and with more features you might want to take a look at a visitor comment that contains DisableKeys.js script that gives you more control and you can disable almost any key and/or key combination, including ctrl + key and alt + key.

44 Comments

likon January 15th, 2008 at 11:45 am

Hey, did you think about what hapends when user turn off javascript? he can then copy everything.

Vijay December 26th, 2007 at 4:52 pm

Thank U … I made some changes for me….

Itz working fine

Regards
Vijay

Raju October 27th, 2007 at 2:34 pm

hi,
Gud work, and thanks for sharing your effort

Can any one tell me what changes i have to make to this script to disable Ctrl+X combination on the whole page?

krishnamurthy.s October 16th, 2007 at 4:25 pm

it is working nice

Thanks
Krishnamurthy.s
HCLTechnlogy

s.selvaraj October 9th, 2007 at 5:21 pm

i want disabled my page when on button press in my screen .It is possible?

nishant October 8th, 2007 at 9:27 am

hi all!!

this code is not working in ie7, i checkd it in opera there its alright….but in ie7 it is not displaying message which i want to when ctrl is pressed

kim September 17th, 2007 at 6:11 pm

if i want to use this code for an entire page’s content that is enclosed within an iframe, how would one go about doing so?

thanks!

naveen August 23rd, 2007 at 8:30 am

disable Ctrl+N option from (File -> New -> Window(Ctrl+N) in Internet Explorer) using javascript

kapi August 13th, 2007 at 8:37 pm

Hi Pig,

Thanks for this excellent script. Could you please tell me how I can do same for f4 etc keys.

Warm Regards,
Kapi

EsaS August 5th, 2007 at 12:15 pm

continue:

var message=”";
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers)
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
document.oncontextmenu=new Function(“return false”)

EsaS August 5th, 2007 at 12:09 pm

to disable mouse and other key compination, just load page without properties, like above where url is called by this. openpage(“index.html”);
script in body tag. You also get more space to
show more on screen
function openapge(url){
var wid = screen.width;
var hei = screen.height-70;
var win7120=window.open(url,”ikkuna43″,’toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=’+wid+’,height=’+hei+”);
moveTo(0,0);}

and this to prevent middle and right mouse click

Jaryd2006 August 2nd, 2007 at 8:28 am

Hi im just wondering, Is it possible to use javascript to make like an onkeypress function to insert text like say a password into somewhere when I press like ctrl shift P or something? As in I press Ctrl shift P (or whatever else i choose) and it inserts my password into a box?

vineet July 23rd, 2007 at 8:47 am

hi i have written a html page like this, but it give me the error “object expected” when i try to press control key. Pls suggest me the answer for the same.

function disableCtrlKeyCombination(e)
{
//list all CTRL + key combinations you want to disable
var forbiddenKeys = new Array(‘a’, ‘n’, ‘c’, ‘x’, ‘v’, ‘j’);
var key;
var isCtrl;

if(window.event)
{

key = window.event.keyCode; //IE
if(window.event.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}
else
{
key = e.which; //firefox
if(e.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}

//if ctrl is pressed check if other key is in forbidenKeys array
if(isCtrl)
{
for(i=0; i

hi how r u it?

John July 18th, 2007 at 3:58 am

Thx man, this works like a charm…..

mukesh July 11th, 2007 at 11:08 am

i use this script in framset i not get result there is any other script r mothed to use in framset

Tarun May 17th, 2007 at 9:37 am

RAM: “This is very helpful. But, i need to disable the the Shift+PrintScreen in my webpage.
Because, it will avoid the user to copy the page (i.e. screenshots).

You can use window.event.shiftKey for Internet Explorer

Tarun May 17th, 2007 at 9:31 am

Thanks Slobodan …you saved a day

Slobodan Kovacevic April 12th, 2007 at 9:57 am

Ram I don’t think that’s possible. For example, on my Ubuntu (Linux) I can get a screen shot of my browser even though it’s not active window. In other words, I can get a screen shot regardless of what browser does.

Ram April 12th, 2007 at 9:17 am

This is very helpful. But, i need to disable the the Shift+PrintScreen in my webpage.
Because, it will avoid the user to copy the page (i.e. screenshots).

namrata March 23rd, 2007 at 8:01 pm

how to disable right click which invokes the contect menu?

y0shi March 21st, 2007 at 8:27 am

works perfect! :) thanks alot

Parker March 3rd, 2007 at 8:06 pm

Dev – to disable back button:

if (document.all) {
history.forward();
}
else {window.forward();
}

To trigger back button:

document.formName.buttonName.onClick = “if (document.all) {history.back();} else {window.back(); }”

To load a specific page when back button pressed, place this in a script in the of the page you are going back to:

if (history.next) {document.location=”http://defaultpage.htm”;}
}

although history.next does not work with FF

JDROO February 3rd, 2007 at 1:35 am

is there a way to disable them with html? im looking and can’t find a thing >.

ja January 17th, 2007 at 2:13 pm

i dnt think tht can be done dude.

Dev January 17th, 2007 at 11:39 am

hi,
i dnt know whether its the rite thread …
i want to disable (or customize) my browser(IE) Back button.
is there any way to trigger back button click event?
or is there any way to edit the history stack?
or is there any way to load a specific page always when
the back button is clickeD? lots of Questions for which i
searched a lot and couldnt find any answer yet… :(
any one pl help me..

thanks,
Dev

Slobodan Kovacevic January 17th, 2007 at 9:01 am

I am not sure that you can disable menu in IE, at least I never seen it. It would be strange for user if a Java Script would be able to block his menu options (for example, that would mean that you can block File -> Quit, so user cannot close the browser). Not to mention that this would be huge usability issue.

If you really need this perhaps you should try opening a page in a popup that doesn’t have menu at all.

Dev January 17th, 2007 at 8:56 am

Hello thank You very much for the Disable Ctrl + N code :)
Its just working GR8!!!

To go further on this, is there any way to disable the

” File > New > Window ” Option from Internet Explorer??

Thanks,
Dev

Miguel Palizada January 12th, 2007 at 12:21 am

Does anybody knows how to clear the clipboard with javascript since firefox? The script is really amazing however Ctrl+C, Ctrl+Insert, Edit + Copy and mouse´s right click + copy is used to Copy and Ctrl+V, Shif+Insert, Edit + Paste and mouses´s rigth click + paste is used to Paste….. so, how can i prevent the Copy/Paste issue for all this combinations?

I´ll be gratefull if you can help me!!

Stanislav December 29th, 2006 at 3:59 pm

HEY
I was trying to disable CTRL + A….it works only in IE.
Do anybody have simple code wich works in Firefox 1.7
and any other browser
Help me please

Basti December 24th, 2006 at 12:27 pm

@jordan – I am not sure if that’s possible. On most operating systems crtl+alt+delete have some special meaning and it just might be possible that you cannot override that using JavaScript.

If you happen to find a solution please let us know.

jordan December 24th, 2006 at 12:21 pm

plz help me i need to disable ctrl alt delete …..does any one have any script for that???

Omar December 11th, 2006 at 6:09 pm

Steve,

You can do that by adding the function calls to the Body tag only:

<body
onkeypress=”return disableCtrlKeyCombination(event);”
onkeydown=”return disableCtrlKeyCombination(event);” >

</body>

Omar December 11th, 2006 at 6:08 pm

Steve,

You can do that by adding the function calls to the Body tag only:

Steve December 8th, 2006 at 6:11 pm

QUOTE: “Although the key combinations are disabled only on one field the same script can be easily modified to disable ctrl + key combinations on whole page.”

For us beginners it would be handy if you told us how to do this. I want to protect the body text only on one particular page on my site

webclown October 18th, 2006 at 8:13 am

I like the script it is eay to follow but how can I disable all keys except for the number keys
in the number pad or on the key board

garry loyder August 23rd, 2006 at 5:21 pm

it should work . did you even try this code?
do you have a working example? thanks.

sivabalan August 21st, 2006 at 8:05 am

can u explain me how to modify this script
fordisable ctrl + key combinations on whole page ?

carmella July 22nd, 2006 at 1:18 am

A thousand thankyous Basti & pig! I searched for this on 7/21/2006 and see you posted yours just in the nick of time for me. It works great and by studying what you’ve done, I’ve learned some things. Thanks for taking the time to share what you’ve figured out with the rest of us.

Basti July 17th, 2006 at 9:46 am

@Rhaine – you don’t put these functions in onLoad. You put it in element in which you want to disable ctrl + key combinations. That might be a body tag (although I never tried it, but I presume it wouldn’t be a problem) or some text box. Either way you should make it look something like this:

<input type=“text” name=“mytext”
onKeyPress=“return disableCtrlKeyCombination(event);”
onKeyDown=“return disableCtrlKeyCombination(event);” />

Rhaine July 15th, 2006 at 7:35 pm

pig.. what is the complete script for this one? why does
the browser tell me the ctrlkey is not an object? i tried your script…
please elaborate.. thanks

Rhaine July 15th, 2006 at 5:09 pm

where will you put this function? will you put it on onLoad for it wo work please reple…. tnx…

pig June 18th, 2006 at 4:13 am

I wrote something very similar to this a couple of months ago, basically the same thing just a little more compact.

function disableCtrlModifer(evt)
{
var disabled = {a:0, c:0, x:0, v:0};
var ctrlMod = (window.event)? window.event.ctrlKey : evt.ctrlKey;
var key = (window.event)? window.event.keyCode : evt.which;
key = String.fromCharCode(key).toLowerCase();
return (ctrlMod && (key in disabled))? false : true;
}