menu

Questions & Answers

How to initiate a keyboard key click event in JavaScript?

Here I'm trying to override the default shortcuts of IE browser. I'm using IE8 for my application.

Let me give an example. I want to override a shortcut ALT+P. The default function of ALT+P in IE is "Open the Page menu". I tried doing the following way, but I'm not able to override it:

    <script type="text/javascript"> 
 
document.onkeydown = printKeycode;
var isAlt = false;
 
function printKeycode(e) 
{
    //alert('11')
    var keycodeentered = window.event.keyCode;
    if(keycodeentered == 18) 
    {
        isAlt=true;
    }
 
    if(keycodeentered == 80 && isAlt == true)  // P keycode is 80
    {
        alert('Combination entered');
        window.event.returnValue = false;
    
    }
}
 
 </script>

But I'm not able to override the default functionality. My action is being triggered at the same time the default action is happening. Small observation is, I am able to prevent the default action of shortcuts using CTRL combination. Any clue?

Answers(1) :