0

I have this code, which is supposed to press "W" multiple times when "E" is pressed, it works well in fire fox but i couldn't get it to run on chrome, i keep getting an error saying

Uncaught TypeError: document.onkeydown is not a function

$.ajax({
url : 'https://greasyfork.org/en/scripts/14564-%D8%B6%D8%A7%D8%BA%D8%B7-%D8%A7%D9%84%D8%AF%D8%A8%D9%84%D9%8A%D9%88',
datatype : 'html',
success : function(data){
    var version = $(data).find('.script-show-version:eq(1) span').text();
    if (version !== '0.1'){
        var z = confirm('There is an update');
        if (z === true){
            window.open('https://greasyfork.org/scripts/14564-%D8%B6%D8%A7%D8%BA%D8%B7-%D8%A7%D9%84%D8%AF%D8%A8%D9%84%D9%8A%D9%88/code/%D8%B6%D8%A7%D8%BA%D8%B7%20%D8%A7%D9%84%D8%AF%D8%A8%D9%84%D9%8A%D9%88.user.js','_self');
        }
    }else{
        (function() {
        var amount = 20;
        var duration = 50; //ms

        var overwriting = function(evt) {
            if (evt.keyCode === 69) { // KEY_E
                for (var i = 0; i < amount; ++i) {
                    setTimeout(function() {
                        document.onkeydown({keyCode: 87}); // KEY_W
                        document.onkeyup({keyCode: 87});
                    }, i * duration);
                }
            }
        };

        window.addEventListener('keydown', overwriting);
    })();
    }
}});
  • It's indeed not a function as per [the specification](https://developer.mozilla.org/en-US/docs/Web/API/Document). It's a property that can be assigned a function which will be fired when a key is pressed. Check the answers in the duplicate question. – wOxxOm Dec 20 '15 at 07:00
  • Possible duplicate of [Firing a keyboard event on Chrome](http://stackoverflow.com/questions/1897333/firing-a-keyboard-event-on-chrome) – wOxxOm Dec 20 '15 at 07:01
  • how do you suggest I make it work – karim awad Dec 20 '15 at 07:07
  • I suggested looking at the answers in the duplicate question, there are many different methods. – wOxxOm Dec 20 '15 at 07:12

0 Answers0