Fire Keys in Java Script

Santosh4u
Posted by Santosh4u under JavaScript category on | Views : 2402
Description:
Just Drag a Text Box and Focus the Curser inside the Textbox.
Then Press the Combined Key.
<html>
<head>
<script language="JavaScript">
function CtrlKeys(e)
{
//Create an array
var arrCtrlKeys = new Array('a', 'n', 'c', 'x', 'v', 'j');
var key;
var isCtrl;

if(window.event)
{
key = window.event.keyCode;
if(window.event.ctrlKey)
isCtrl = true;
else
isCtrl = false;
}

else
{
key = e.which;
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<arrCtrlKeys .length; i++)
{
//case-insensitive comparation
if(arrCtrlKeys[i].toLowerCase() == String.fromCharCode(key).toLowerCase())
{
alert('You have pressed CTRL + '
+String.fromCharCode(key)
+'.');

return false;
}
}
}
return true;
}
</script>

</head>
<body>
<form method="">
<div>
<h3>Test Key Combinition [Ctrl+A, Ctrl+N, Ctrl+C, Ctrl+X, Ctrl+V,Ctrl+J]</h3>
<input type="text" name="mytext"
onKeyPress="return CtrlKeys(event);"
onKeyDown="return CtrlKeys(event);" />
</div>
</body>
</html>

Regards
Santosh

Comments or Responses

Login to post response