1

Got another chat bot process I am trying to learn. Okay, so I now have a javascript chat bot that connects to a websocket chat room and functions normally. I have gotten it to respond to commands

ex

if (text === "!ping" && (user === "user" || isStaff || isOwner || isSub)) {
    channel.sendMessage("pong");
}

What I am trying to do now is take a command such as "!up" and translate that into the bot pressing the "up" arrow on the keyboard inside of another program.

I am not sure of how to get started in this. Every time I try to google it, all I get is how to read keyboard events when someone enters a key into a text box. I am new to javascript so I am unaware of there being an exact name for what it is that I am trying to do. If someone could at least point me into the right direction as to what it is I need to look up in order to learn to do it, I would be very grateful :)

Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
Layne Mayo
  • 39
  • 10

1 Answers1

1

You can use jQuery to simulate these events

Let's say that you want to press up key within <p id="someid"></p> tag of a program.

the codes are:

37  left
38  up
39  right
40  down

You can find codes for other keys through a simple google search

Now if you want to press 'up' arrow, then:

if (text === "!up" && (user === "user" || isStaff || isOwner || isSub)) {
    //this function will trigger keyup event
    $(function() {
        var e = $.Event('keypress');
        e.which = 38; // 38 is code for up arrow. 
        $('#someid').trigger(e);
        //you can provide id or class of element where you want this event
        //to be triggered
    });
}

see also:

  1. Trigger Keypress with jQuery
  2. Definitive way to trigger keypress events with jQuery
  3. fiddle - press 'M' key on click of a button
Community
  • 1
  • 1
Raman Sahasi
  • 30,180
  • 9
  • 58
  • 71
  • Ohh okay, thanks! I didn't even think about jQuery to be honest. Probably should have looked there first. – Layne Mayo Aug 21 '16 at 04:59
  • @Layne No worries. I think it will definitely work with your requirements. – Raman Sahasi Aug 21 '16 at 05:08
  • Actually, I just realized something that I should probably have asked last night. Okay, this chat bot is being ran on my pc, not on a website. I am wanting to take those commands and then have the keypress be done in a separate .exe program window. Will this work with that? I'm just wondering because id & class to me only means css & html, unless program windows have those too and I'm just ignorant haha. – Layne Mayo Aug 21 '16 at 15:02
  • @Layne Mayo Why don't you write your program in [AutoHotKey](https://www.autohotkey.com). It will exactly meet your requirements. I have worked with that before and it's perfect for sending keys to any exe or any other similar process. It's basically created for this purpose only. Just go through some examples and you'll know what I'm saying. – Raman Sahasi Aug 21 '16 at 18:04
  • Okay thanks, I will be sure to check it out when I get home. – Layne Mayo Aug 21 '16 at 18:14