如何挂钩IE键盘事件? [英] How to hook IE keyboard event?

查看:76
本文介绍了如何挂钩IE键盘事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在用户尝试按下swf文件上的某些键组合时禁用浏览器快捷方式。虽然我可以在firefox中实现这一点,但是下面的代码在ie 8中不起作用。如果焦点不在swf文件上,下面的代码可以挂钩键盘事件。但是,我需要的是当用户操作swf文件时挂钩键盘事件。

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;

当焦点在swf文件上时,上面的代码永远不会执行。

Above code never executes when focus is on swf file.

推荐答案

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

这篇关于如何挂钩IE键盘事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆