禁用javascript中的Opera功能键 [英] disable Opera function key in javascript

查看:109
本文介绍了禁用javascript中的Opera功能键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为包含功能键的Web模拟器编写JavaScript代码.用户可以按键盘功能键在模拟器中进行处理,我使用了stopPropagation和preventDefault javascript函数来停止/取消浏览器调用其功能键的快捷方式(例如F1将调用Firefox以打开其帮助页面),并且代码运行正常在Firefox和Chrome下.但是Opera无法正常工作,Opera调用了我的函数并启动了Opera帮助页面.我在这里使用最新的Opera 10.5.有人可以帮忙吗?

I'm writing javascript code for a web emulator containing function keys. The user is able to press keyboard function keys to process something in the emulator, I used stopPropagation and preventDefault javascript function to stop/cancel browsers invoking their function key shortcut (e.g. F1 will invoke Firefox to open its help page) and the code works fine under Firefox and Chrome. But Opera didn't work, Opera invokes both my function and launches Opera help page. I'm using latest Opera 10.5 here. Can anyone help ?

此代码位于FireFox javascript句柄中,并附加在主体HTML onkeydown事件中:

this code is in FireFox javascript handle and this is attached in main body HTML onkeydown event:

function KeyHandler(ev)
{
   if(112==ev.keyCode) {InvokeF1();return CancelKey(ev);}
   if(113==ev.keyCode) {InvokeF2();return CancelKey(ev);}
   ...
}

function CancelKey(ev)
{
    ev.stopPropagation();
    ev.preventDefault();
}

IE为CancelKey使用了不同的代码,这些代码使用ev.CancelBubble = true和ev.returnValue = false.

IE used different code for CancelKey, which use ev.CancelBubble = true and ev.returnValue = false.

推荐答案

要防止Opera中按键的默认操作,您需要在keypress事件中执行此操作.以下内容改编自我对类似问题的回答:

To prevent the default action of a keypress in Opera, you need to do it in the keypress event. The following is adapted from my answer to this similar question:

var cancelKeypress = false;

document.onkeydown = function(evt) {
    evt = evt || window.event;
    cancelKeypress = /^(112|113)$/.test("" + evt.keyCode);
    if (cancelKeypress) {
        return false;
    }
};

/* For Opera */
document.onkeypress = function(evt) {
    if (cancelKeypress) {
        return false;
    }
};

这篇关于禁用javascript中的Opera功能键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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