在JavaScript中触发键盘事件

时间:2009-06-07 08:49:35

标签: javascript javascript-events safari webkit keyboard-events

我正在尝试使用JavaScript在Safari中模拟键盘事件。

我试过这个:

var event = document.createEvent("KeyboardEvent");
event.initKeyboardEvent("keypress", true, true, null, false, false, false, false, 115, 0);

......还有这个:

var event = document.createEvent("UIEvents");
event.initUIEvent("keypress", true, true, window, 1);
event.keyCode = 115;

然后,尝试这两种方法后,我遇到了同样的问题:执行代码后,事件对象的keyCode / which属性设置为0,而不是115

有谁知道如何在Safari中可靠地创建和发送键盘事件? (如果可能的话,我宁愿用纯JavaScript实现它。)

5 个答案:

答案 0 :(得分:32)

我正在研究DOM Keyboard Event Level 3 polyfill。在最新的浏览器中或使用此polyfill,您可以执行以下操作:

element.addEventListener("keydown", function(e){ console.log(e.key, e.char, e.keyCode) })

var e = new KeyboardEvent("keydown", {bubbles : true, cancelable : true, key : "Q", char : "Q", shiftKey : true});
element.dispatchEvent(e);

//If you need legacy property "keyCode"
// Note: In some browsers you can't overwrite "keyCode" property. (At least in Safari)
delete e.keyCode;
Object.defineProperty(e, "keyCode", {"value" : 666})

更新:

现在我的polyfill支持遗留属性" keyCode"," charCode"和""

var e = new KeyboardEvent("keydown", {
    bubbles : true,
    cancelable : true,
    char : "Q",
    key : "q",
    shiftKey : true,
    keyCode : 81
});

示例here

此外,这里是与我的polyfill分开的跨浏览器initKeyboardEvent:(gist)

Polyfill demo

答案 1 :(得分:28)

您是否正确派遣了活动?

function simulateKeyEvent(character) {
  var evt = document.createEvent("KeyboardEvent");
  (evt.initKeyEvent || evt.initKeyboardEvent)("keypress", true, true, window,
                    0, 0, 0, 0,
                    0, character.charCodeAt(0)) 
  var canceled = !body.dispatchEvent(evt);
  if(canceled) {
    // A handler called preventDefault
    alert("canceled");
  } else {
    // None of the handlers called preventDefault
    alert("not canceled");
  }
}

如果你使用jQuery,你可以这样做:

function simulateKeyPress(character) {
  jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}

答案 2 :(得分:15)

这是由于Webkit中的bug

您可以使用createEvent('Event')而不是createEvent('KeyboardEvent')解决Webkit错误,然后分配keyCode属性。请参阅this answerthis example

答案 3 :(得分:8)

Mozilla Developer Network提供以下说明:

  1. 使用event = document.createEvent("KeyboardEvent")
  2. 创建活动
  3. 初始化keyevent
  4. 使用:

    event.initKeyEvent (type, bubbles, cancelable, viewArg, 
           ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, 
               keyCodeArg, charCodeArg)
    
    1. Dispatch the event使用yourElement.dispatchEvent(event)
    2. 我没有在你的代码中看到最后一个,也许这就是你所缺少的。我希望这也适用于IE ......

答案 4 :(得分:1)

我对此并不是很好,但KeyboardEvent =>见KeyboardEvent  用initKeyEvent初始化。
以下是在<input type="text" />元素上发出事件的示例

&#13;
&#13;
document.getElementById("txbox").addEventListener("keypress", function(e) {
  alert("Event " + e.type + " emitted!\nKey / Char Code: " + e.keyCode + " / " + e.charCode);
}, false);

document.getElementById("btn").addEventListener("click", function(e) {
  var doc = document.getElementById("txbox");
  var kEvent = document.createEvent("KeyboardEvent");
  kEvent.initKeyEvent("keypress", true, true, null, false, false, false, false, 74, 74);
  doc.dispatchEvent(kEvent);
}, false);
&#13;
<input id="txbox" type="text" value="" />
<input id="btn" type="button" value="CLICK TO EMIT KEYPRESS ON TEXTBOX" />
&#13;
&#13;
&#13;