How to hook IE keyboard event?

2019-05-07 18:38发布

问题:

I am trying to disable browser shortcuts while user tries to press some combination of keys on the swf file. Although I can achieve this in firefox, below code does not function in ie 8. Below code is able to hook the keyboard events if focus is not on the swf file. However, what I need is hooking keyboard events when user operates on swf file.

function hookKeyboardEvents(e) {

    alert("hooked key");
    // get key code
    var key_code = (window.event) ? event.keyCode : e.which;

    // case :if it is IE event
    if (window.event)
    {
        if (!event.shiftKey && !event.ctrlKey && !event.altKey) {
            window.event.returnValue = null;
            event.keyCode=0;
        }       
    }
    // case: if it is firefox event
    else
        e.preventDefault();

    document[flashId].keyDown(key_code);
}

window.document.onkeydown = hookKeyboardEvents;

Above code never executes when focus is on swf file.

回答1:

document.body.accessKey = String.fromCharCode(e.keyCode);