0

I am new to JavaScript and programming in general. I will try to explain what I want to do as best I can. I have a very similar question to this guy, but I would like to do it in JavaScript as I somewhat know how to use it. Need to send key presses to a webpage(html5 game)

Basically I want to be able to send key presses and/or mouse clicks I.E. WASD or up, down, left, and right. So I could effectively automate a task.

As a side note to the main question, is there a way to show JavaScript events that are happening in a game, so that it could react to them?

If it is possible how would I get the script to effect the webpage, and what would I need to know to do it?

Thanks!

Raven
  • 221
  • 1
  • 4
  • 14
  • What are you trying to achieve with that? – Oriol May 23 '15 at 19:08
  • @Oriol I want to make a bot. – Raven May 23 '15 at 19:09
  • What have you tried so far? Did you googled about it? Javascript event listeners will certainly solve your problem. – rafaelcastrocouto May 23 '15 at 19:12
  • take a look here: http://phaser.io/ – Papaya Labs May 23 '15 at 19:13
  • The page you are trying to trick does something to detect key presses. To fake key strokes, you must know what that "something" is. Probably, it's an event listener, you must know the event type, the phase, the element... – Oriol May 23 '15 at 19:16
  • http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript? – Firedrake969 May 23 '15 at 19:18
  • @rafaelcastrocouto I have found these, http://stackoverflow.com/questions/29719419/can-javascript-press-the-enter-key-for-me and http://stackoverflow.com/questions/961532/firing-a-keyboard-event-in-javascript but i have no how to get them to work. I don't even know how to get the code to intereact with a webpage. Do i use the console? Some other software? idk... – Raven May 23 '15 at 19:20
  • @Firedrake969 Yes i saw that, I don't really understand how to put it into use though. – Raven May 23 '15 at 19:23
  • Do you care if you have to use a library? – Firedrake969 May 23 '15 at 19:28
  • @Firedrake969 I am not entirely sure. I don't see why not though. – Raven May 23 '15 at 19:32
  • possible duplicate of [Trigger a keypress/keydown/keyup event in JS/jQuery?](http://stackoverflow.com/questions/3368578/trigger-a-keypress-keydown-keyup-event-in-js-jquery) – AstroCB May 23 '15 at 19:59

2 Answers2

1

Assuming you're fine with using jQuery, as indicated in the comments, you could so something like this:

function simulateKey(char) {
    var e = $.Event("keydown", { keyCode: char.charCodeAt(0)});
    $("body").trigger(e);
}

Also assuming you want a keydown event and it's triggered on the body (you can change the body to whatever selector).

Firedrake969
  • 763
  • 8
  • 22
0

Your keyboard/mouse event listeners should be calling different functions instead of responding to the event directly. Generally speaking, event listeners should extract information from the event and pass it to methods to handle state changes. So your 'bot' should call the functions that handle the state changes directly instead of faking a key press.

nephiw
  • 1,964
  • 18
  • 38
  • If you still want to dispatch keyboard/mouse events, check out this [post](http://stackoverflow.com/a/5920206/605481) – nephiw May 23 '15 at 19:58