Firing a Keyboard Event in Safari, using JavaScript

前端 未结 5 1179
走了就别回头了
走了就别回头了 2020-11-22 05:22

I\'m trying to simulate a keyboard event in Safari using JavaScript.

I have tried this:

var event = document.createEvent(\"KeyboardEvent\");
event.in         


        
相关标签:
5条回答
  • 2020-11-22 05:53

    The Mozilla Developer Network provides the following explanation:

    1. Create an event using event = document.createEvent("KeyboardEvent")
    2. Init the keyevent

    using:

    event.initKeyEvent (type, bubbles, cancelable, viewArg, 
           ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, 
               keyCodeArg, charCodeArg)
    
    1. Dispatch the event using yourElement.dispatchEvent(event)

    I don't see the last one in your code, maybe that's what you're missing. I hope this works in IE as well...

    0 讨论(0)
  • 2020-11-22 06:06

    I am not very good with this but KeyboardEvent => see KeyboardEvent is initialized with initKeyEvent .
    Here is an example for emitting event on <input type="text" /> element

    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);
    <input id="txbox" type="text" value="" />
    <input id="btn" type="button" value="CLICK TO EMIT KEYPRESS ON TEXTBOX" />

    0 讨论(0)
  • 2020-11-22 06:10

    Did you dispatch the event correctly?

    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");
      }
    }
    

    If you use jQuery, you could do:

    function simulateKeyPress(character) {
      jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
    }
    
    0 讨论(0)
  • This is due to a bug in Webkit.

    You can work around the Webkit bug using createEvent('Event') rather than createEvent('KeyboardEvent'), and then assigning the keyCode property. See this answer and this example.

    0 讨论(0)
  • 2020-11-22 06:16

    I am working on DOM Keyboard Event Level 3 polyfill . In latest browsers or with this polyfill you can do something like this:

    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})
    

    UPDATE:

    Now my polyfill supports legacy properties "keyCode", "charCode" and "which"

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

    Examples here

    Additionally here is cross-browser initKeyboardEvent separately from my polyfill: (gist)

    Polyfill demo

    0 讨论(0)
提交回复
热议问题