Chrome中的KeyboardEvent,keyCode为0 [英] KeyboardEvent in Chrome, keyCode is 0

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

问题描述



点击按钮时,在文本框控件上引发事件以模拟keydown事件。事件成功提升,但keyCode为0.我需要将具有正确keyCode的事件对象发送到文本框。
我一直在使用



如何在派发事件中设置keyCode值?

 < HTML> 
< head>
< / head>
< body>
< input type =buttonid =btnvalue =Click>< / input>
< input type =textid =txt>< / input>
< script>
document.getElementById('btn')。addEventListener(click,btnClick);
document.getElementById('txt')。addEventListener(keydown,txtKeydown);

函数btnClick(e){
var txt = document.getElementById('txt');

var dispatchKeyboardEvent = function(target,initKeyboradEvent_args){
var e = document.createEvent(KeyboardEvents);
e.initKeyboardEvent.apply(e,Array.prototype.slice.call(arguments,1));
e.keyCode = 83;
e.charCode = 0;
target.dispatchEvent(e);
};

var cancel =!dispatchKeyboardEvent(txt,$ b $'keydown',true,true,// type,bubbles,cancelable
window,// window
's ',// key
0,// location:0 = standard,1 = left,2 = right,3 = numpad,4 = mobile,5 = joystick
false,false,false); // Ctr,Alt,Shift
}

函数txtKeydown(e){
console.dir(e);
console.log(e.keyCode);
}

< / script>
< / body>
< / html>


解决方案

我很惊讶这个bug一直在年龄。似乎有一个使用通用事件的解决方法。这里有一个演示: http://jsbin.com/awenaq/3


I am trying to set keyCode on dispatched event object .

On button click, event is raised on textbox control to simulating keydown event. Event is raised successfully, but keyCode is 0. I need to send event object with correct keyCode to textbox. I've been using code from here.

How to set keyCode value in dispatched event?

<html>
    <head>      
    </head>
    <body>
        <input type="button" id="btn" value="Click"></input>
        <input type="text" id="txt"></input>
        <script>
            document.getElementById('btn').addEventListener("click", btnClick);
            document.getElementById('txt').addEventListener("keydown", txtKeydown);         

            function btnClick(e) {
                var txt = document.getElementById('txt');               

                var dispatchKeyboardEvent = function(target, initKeyboradEvent_args) {
                    var e = document.createEvent("KeyboardEvents");
                    e.initKeyboardEvent.apply(e, Array.prototype.slice.call(arguments, 1));
                    e.keyCode = 83;
                    e.charCode = 0;
                    target.dispatchEvent(e);
                };

                var canceled = !dispatchKeyboardEvent(txt,
                'keydown', true, true,  // type, bubbles, cancelable
                window,  // window
                's',  // key
                0, // location: 0=standard, 1=left, 2=right, 3=numpad, 4=mobile, 5=joystick
                false, false, false);  // Ctr, Alt, Shift
            }

            function txtKeydown(e) {
                console.dir(e);
                console.log(e.keyCode);
            }

        </script>
    </body>
</html>

解决方案

I am amazed that this bug has been sitting there for ages. There seems to be a workaround using generic events. Here's a demo: http://jsbin.com/awenaq/3

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

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