简介
本文将介绍如何在web页面上禁用特定的按键,如Shift、Ctrl等,以及如何禁用右键菜单。下面介绍的方法适用于当前大多数浏览器。
代码实现
定义javascript 函数,并且将其作为页面body的onload事件的处理函数。
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript">
function restrainIllegalOperation(){
document.onkeydown = keyEventHandle;
document.oncontextmenu = contextEventHandle;
}
function keyEventHandle(e) {
// The information under keys is registered.
var shift, ctrl, alt;
// Mozilla(Firefox, NN) and Opera
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;
// Internet Explorer
} else {
keycode = event.keyCode;
ctrl = event.ctrlKey;
shift = event.shiftKey;
alt = event.altKey;
}
// Ctrl + R
if((ctrl && keycode == 82) || (ctrl == 17 && keycode == 82)) {
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
// Internet Explorer
} else {
// The higher rank propagation of an event is prevented.
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
// Ctrl + F5
if((ctrl && keycode == 116) || (ctrl == 17 && keycode == 116)) {
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
// Internet Explorer
} else {
// The higher rank propagation of an event is prevented.
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
// F5
if(keycode == 116) {
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
// Internet Explorer
} else {
// The higher rank propagation of an event is prevented.
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
// Alt
if((alt && keycode == 37) || (alt == 18 && keycode == 37)) {
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
}
return false;
}
// ESC
if(keycode == 27) {
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
// Internet Explorer
} else {
// The higher rank propagation of an event is prevented.
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
// BackSpace
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;
}
}
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// The higher rank propagation of an event is prevented.
e.which = 0;
e.preventDefault();
e.stopPropagation();
// Internet Explorer
} else {
// The higher rank propagation of an event is prevented.
event.keyCode = 0;
event.returnValue = false;
event.cancelBubble = true;
}
return false;
}
// Mozilla(Firefox, NN) and Opera
if (e != null) {
// In MacOS, Cmd+R (renewal of a Web page and cash) deters.
if (e.metaKey && keycode == 82) {
return false;
}
// In MacOS, it is Cmd+. Control of [(it moves to a front page)
if (e.metaKey && keycode == 219) {
return false;
}
}
}
function contextEventHandle() {
return false;
}
</script>
<title></title>
</head>
<body onload="restrainIllegalOperation()">
Prevent special keys and context menu.
</body>
</html>
本文译自Prevent user hit some special keywords(Ctrl, Shift...) and access context menu on webpage