Hi this is javascript code to disable ctrl + key combinations,
onkeydown is the event used for if user press keys on the body then disableCtrlKeyCombination() will called and checks for the forbiddenKeys
if Ctrl + forbiddenKey matched then automatically alert msg will show,
<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', 'w', 't', 'p');
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>
<body onkeydown="disableCtrlKeyCombination(event)">
</body>
onkeydown is the event used for if user press keys on the body then disableCtrlKeyCombination() will called and checks for the forbiddenKeys
if Ctrl + forbiddenKey matched then automatically alert msg will show,
<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', 'w', 't', 'p');
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>
<body onkeydown="disableCtrlKeyCombination(event)">
</body>
0 comments:
Post a Comment