如何在Chrome中用Javascript模拟按键

如何在Chrome中用Javascript模拟按键,javascript,google-chrome,dom-events,keyboard-events,Javascript,Google Chrome,Dom Events,Keyboard Events,我在这个话题上找到了很多信息,但似乎没有一个有用 我想做的是在Chrome59中模拟按键(向下/按下/向上)事件(我真的不关心其他浏览器)。模拟事件本身不是问题,但问题是我没有找到向后兼容并包含charCode/keyCode值的解决方案。如果由我决定,我会更新另一个应用程序的代码以检查密钥/代码,但不幸的是,这不是由我决定的 到目前为止,我所尝试的: var evt = new KeyboardEvent(type, {code: options.charCode, key: options.

我在这个话题上找到了很多信息,但似乎没有一个有用

我想做的是在Chrome59中模拟按键(向下/按下/向上)事件(我真的不关心其他浏览器)。模拟事件本身不是问题,但问题是我没有找到向后兼容并包含charCode/keyCode值的解决方案。如果由我决定,我会更新另一个应用程序的代码以检查密钥/代码,但不幸的是,这不是由我决定的

到目前为止,我所尝试的:

var evt = new KeyboardEvent(type, {code: options.charCode, key: options.keyCode, keyCode: options.keyCode, charCode: options.keyCode, which: options.which});
element.dispatchEvent(evt);
根据这一点,应该可以工作并创建一个设置了keyCode/charCode/的事件,但它不工作:

我的代码(我尝试了带引号和不带引号以及用字符代替数字):

输出:

KeyboardEvent {isTrusted: false, key: "123", code: "123", location: 0, ctrlKey: false…}
altKey:false
bubbles:false
cancelBubble:false
cancelable:false
charCode:0
code:"123"
composed:false
ctrlKey:false
currentTarget:null
defaultPrevented:false
detail:0
eventPhase:0
isComposing:false
isTrusted:false
key:"123"
keyCode:0
location:0
metaKey:false
path:Array(0)
repeat:false
returnValue:true
shiftKey:false
sourceCapabilities:null
srcElement:null
target:null
timeStamp:2469092.6000000006
type:"keypress"
view:null
which:0
__proto__:KeyboardEvent
然后我看了一下规范,发现keyCode/charCode/which属性已经不存在了,这让我相信它已经从标准中删除了,它不应该再像这样工作了:

由于这显然不起作用,我开始研究不推荐使用的函数initKeyboardEvent,并尝试了以下方法:

var evt = document.createEvent("KeyboardEvent");
//Chrome hack
Object.defineProperty(evt, 'keyCode', {
    get : function(){
        return this.keyCodeVal;
    }
});
Object.defineProperty(evt, 'which', {
    get : function(){
        return this.keyCodeVal
    }
});
Object.defineProperty(evt, 'charCode', {
    get : function(){
        return this.charCodeVal
    }
});
//initKeyBoardEvent seems to have different parameters in chrome according to MDN KeyboardEvent(sorry, can't post more than 2 links), but that did not work either
evt.initKeyboardEvent("keypress",
    true,//bubbles
    true,//cancelable
    window,
    false,//ctrlKey,
    false,//altKey,
    false,//shiftKey,
    false,//metaKey,
    123,//keyCode,
    123//charCode
);
evt.charCodeVal = 123;
evt.keyCodeVal = 123;
这看起来很有希望,但当我调度事件时,我可以在处理程序中看到这个事件:

KeyboardEvent
altKey:false
bubbles:true
cancelBubble:false
cancelable:true
charCode:0
code:""
composed:false
ctrlKey:false
currentTarget:input#iceform:username.iceInpTxt.bookLoginTextbox
defaultPrevented:false
detail:0
eventPhase:2
isTrusted:false
key:""
keyCode:0
location:0
metaKey:true
path:Array[16]
repeat:false
returnValue:true
shiftKey:true
sourceCapabilities:null
srcElement:input#iceform:username.iceInpTxt.bookLoginTextbox
target:input#iceform:username.iceInpTxt.bookLoginTextbox
timeStamp:9932.905000000002
type:"keypress"
view:Window
which:0
__proto__:KeyboardEvent
这就是我最终放弃并决定寻求帮助的时候。是否有任何方法可以正确地模拟这些事件,使其也向后兼容


请不要建议JQUERY或类似的东西

好的,我终于解决了问题。代码本身实际上是正常的(带有不推荐的initKeyboardEvent的代码)


问题是,我是在chrome扩展的contentscript中执行此操作的。在这种情况下,事件的所有属性都重置为默认值,然后引发事件。现在,我正在向页面添加一个脚本,该脚本包含完全相同的代码,并且工作正常。

有时,在这些情况下,会有不同的方法。你想达到什么目的?哈哈,你是对的,没有看到错误的标签。。。我试图模拟关键事件,以模拟真实用户单击页面中的元素
KeyboardEvent
altKey:false
bubbles:true
cancelBubble:false
cancelable:true
charCode:0
code:""
composed:false
ctrlKey:false
currentTarget:input#iceform:username.iceInpTxt.bookLoginTextbox
defaultPrevented:false
detail:0
eventPhase:2
isTrusted:false
key:""
keyCode:0
location:0
metaKey:true
path:Array[16]
repeat:false
returnValue:true
shiftKey:true
sourceCapabilities:null
srcElement:input#iceform:username.iceInpTxt.bookLoginTextbox
target:input#iceform:username.iceInpTxt.bookLoginTextbox
timeStamp:9932.905000000002
type:"keypress"
view:Window
which:0
__proto__:KeyboardEvent