Introduction
This article will show you guys how to prevent user press down some special keys such as Shift, Ctrl.. and access context menu on webpage. It will make sure it will support the most of modern browsers in currently.
Background
Define javascript function and place it in body onload event on your page.
Using the code
Firstly, you have to define a javascript routine to do this job:
function restrainIllegalOperation(){
document.onkeydown = keyEventHandle;
document.oncontextmenu = contextEventHandle;
}
You will place the above routine in body onload event like this:
<body onload="restrainIllegalOperation()">
</body>
Now is the definitions of the two most important routine : keyEventHandle and contextEventHandle.
With keyEventHandle, the are some special keys :Ctrl+R, Ctrl+F5, F5, Alt, ESC, Backspace.
function keyEventHandle(e) {
var shift, ctrl, alt;
if (e != null) {
keycode = e.which;
ctrl = typeof e.modifiers == 'undefined' ? e.ctrlKey : e.modifiers & Event.CONTROL_MASK;
shift = typeof e.modifiers == 'undefined' ? e.shiftKey : e.modifiers & Event.SHIFT_MASK;
alt = typeof e.modifiers == 'undefined' ? e.altKey : e.modifiers & Event.ALT_MASK;
} else {
keycode = event.keyCode;
ctrl = event.ctrlKey;
shift = event.shiftKey;
alt = event.altKey;
}
if((ctrl && keycode == 82) || (ctrl == 17 && keycode == 82)) {
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
} else {
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
if((ctrl && keycode == 116) || (ctrl == 17 && keycode == 116)) {
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
} else {
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
if(keycode == 116) {
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
} else {
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
if((alt && keycode == 37) || (alt == 18 && keycode == 37)) {
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
}
return false;
}
if(keycode == 27) {
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
} else {
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
if(keycode == 8) {
if ((document.activeElement.type == "text") ||
(document.activeElement.type == "textarea") ||
(document.activeElement.type == "password") ||
(document.activeElement.type == "file")) {
if(!document.activeElement.readOnly) {
return true;
}
}
if (e != null) {
e.which = 0;
e.preventDefault();
e.stopPropagation();
} else {
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
if (e != null) {
if (e.metaKey && keycode == 82) {
return false;
}
if (e.metaKey && keycode == 219) {
return false;
}
}
}
The contextEventHandle() routine is so simple like this, just return false.
function contextEventHandle() {
return false;
}
Points of Interest
This is useful when woring with Japanese clients.
History
First post on Dec 26, 2012