Opera中的KeyPress故障 [英] KeyPress malfunction in Opera

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

问题描述

我正在使用以下代码在JavaScript中检测用户的按键操作:

I'm using the following code to detect users' key pressing, in JavaScript:

$(document).bind('keydown', function (event) {
    'use strict';
    var keyCode = event.keyCode;

        switch (keyCode) {
        case '{N}':
             doSomething();
             break;

        default:
             break;
        }
});

其中doSomething是先前定义的函数,而{N} JavaScript字符代码.

Where doSomething is a previously defined function and {N} is any of the JavaScript Char Codes.

它在所有主流浏览器中都能正常运行,但是在 Opera 中,即使按下某个键,它也只会调用doSomething一次,而不是直到这样做,才释放密钥. 我该怎么做才能解决此问题?

It works properly in every major browser, but in Opera even if a key remains pressed, it only calls doSomething once, instead of doing it until the key is released. What can I do to fix this?

我使用keypress事件而不是keydown(Opera无法很好地处理)来解决它.

I solved it using the keypress event instead of keydown (which is not well handled by Opera).

推荐答案

这是一个已知的错误,应(最终!)应尽快修复.简而言之,按键事件不会重复,而按键事件却会重复.如果您想重复操作(而不关心像大多数功能键一样在所有浏览器中都不触发键的键),则应该改用听键按压,这是一种合理的跨浏览器解决方案.

this is a known bug which should (finally!) get fixed soon. In short, keydown events are not repeated while keypress events are. Listening to keypress instead if you want repetition (and don't care about keys that do NOT fire keypress in all browsers like most function keys) should be a reasonable cross-browser solution.

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

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