Searcher Searcher - 1 month ago 6
Javascript Question

how to find double key strokes in javascript such as ctrl+t ctrl+K

I want to know which keys were pressed along with the ctrl key or any key. For this i wrote like this

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
<script type="text/javascript">
function onkeyPressEvent(e) {
var keycode;
var keyname;
if(window.event)
{
if(e.keyCode && e.ctrlKey)
alert("ctrl + key Pressed");
//keycode = e.keyCode;
}
//keyname = String.fromCharCode(keycode);

//if(keyname.length!=0)
//alert(keyname)
}

</script>
</head>
<body onkeypress="onkeyPressEvent(event)">
<form id="form1">
<div></div>
</form>
</body>
</html>


But this is not working properly. In IE if we click on Ctrl+T it is opening new tab. how to disable these type of short cuts in our programming using javascript. I want a alert box stating that which key was pressed along with ctrl.

Thanks,

Answer

Here is your code..

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
    <script type="text/javascript">
        function onkeyDownEvent(e) {
            var keycode;
            var keyName="";
            if(window.event) 
            {
                if(e.ctrlKey)
                    keyName = "Ctrl";
                else  if(e.altKey)
                    keyName ="Alt";
                else if(e.shiftKey)
                    keyName = "Shift";

                if((e.ctrlKey||e.altKey||e.shiftKey) && (e.keyCode<16 || e.keyCode>18))
                        alert(keyName +" + "+ String.fromCharCode(e.keyCode) );
            }
        }

    </script>
</head>
<body onkeydown="onkeyDownEvent(event)">
    <form id="form1">
    <div></div>
    </form>
</body>
</html>
Comments