Layne Mayo
Layne Mayo

Reputation: 39

Making a javascript chat bot turn chat commands into key presses in another program

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

Upvotes: 1

Views: 1390

Answers (1)

Raman Sahasi
Raman Sahasi

Reputation: 31851

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

Upvotes: 1

Related Questions