Javascript - simulate key events on Chrome 53

2020-03-24 06:23发布

I am trying to simulate key event (press) on Chrome 53. All the solutions that I found on StackOverflow seems not to be working..

My goal is to have a function that gets a keyCode and simulates a key press with it - Pure JS is required

function keyPressSimulate(keyCode) {...?}

Code samples that I already tried:

Node.prototype.fire=function(type,options){
     var event=new CustomEvent(type);
     for(var p in options){
         event[p]=options[p];
     }
     this.dispatchEvent(event);
}

 document.fire("keyup",{ctrlKey:true,keyCode:90,bubbles:true})

Another one:

presskey:  function(k) {
                var e = new Event("keydown");
                e.keyCode= k;
                e.which=e.keyCode;
                e.altKey=false;
                e.ctrlKey=true;
                e.shiftKey=false;
                e.metaKey=false;
                document.dispatchEvent(e);
            }

And:

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

And:

presskey:  function(k) {
            var keyboardEvent = document.createEvent("KeyboardEvent");

            var initMethod = typeof keyboardEvent.initKeyboardEvent !== 'undefined' ? "initKeyboardEvent" : "initKeyEvent";


            keyboardEvent[initMethod](
                               "keydown",
                                true,      // bubbles oOooOOo0
                                true,      // cancelable
                                window,    // view
                                false,     // ctrlKeyArg
                                false,     // altKeyArg
                                false,     // shiftKeyArg
                                false,     // metaKeyArg
                                k,
                                0          // charCode
            );

            global.document.activeElement.dispatchEvent(keyboardEvent);
         }

1条回答
冷血范
2楼-- · 2020-03-24 07:12

The keyCode property is deprecated and cannot be set while using the dedicated KeyboardEvent

However, you can use a custom event and set whatever property you want as a 2 step operation (it will be ignored if used in the constructor):

<div id="test" onkeydown="this.textContent = event.keyCode;"></div>
<button onclick="
    var e = new Event('keydown'); 
    e.keyCode = 42; 
    document.getElementById('test').dispatchEvent(e);
    ">click me</button>

Edit as per comments An event is triggered from a user interaction targeting a specific element in the document.

For instance, a user hitting a key on the keyboard is a succession of events (keydown, keyup, keypress). Some of those are captured by the browser to fill in the value of an input field, and then they are transmitted to Javascript as events. There are no feasible way to emulate a user hitting a key from within the Javascript sandbox except than to do it manually (1) detect where the focus is, (2) update the properties of the focussed element based on the default browser behavior, and then (3) trigger the appropriate events on the target element.

查看更多
登录 后发表回答