1

I am coding a twitch bot and I want it so when users type a command (something like !space [time] ) it will randomly press space for the ammount of time they gave. I know how to receive the command but i dont know how to send the key input. Thanks in advance

EDIT : When i say send keypresses i mean from the nodejs console on my end not in on twitch

EDIT 2 : I want this so viewers on my stream can spend points to troll me when i am playing games

Kong
  • 35
  • 1
  • 7
  • 1
    This doesn't make much sense. Twitch bots talk to the server over the network. They send it strings. The server doesn't operate a virtual keyboard. There are no keys to press. – Quentin Jun 16 '18 at 10:17
  • I mean, the user types !space then on my end (cos im hosting the bot) it presses space on my keyboard – Kong Jun 16 '18 at 10:19
  • To do that you'd probably need to find a way to make your bot pretend to be a USB keyboard (and it makes the reference to being a twitch bot largely irrelevant: the problem you are asking about is node simulating keypresses locally and being triggered by a message over the network is beside the point). It's a very odd thing to want and sounds very much like an http://xyproblem.info/ – Quentin Jun 16 '18 at 10:21
  • Added more details – Kong Jun 16 '18 at 10:23
  • Does this answer your question? [Is it possible to simulate keyboard/mouse event in NodeJS?](https://stackoverflow.com/questions/11178372/is-it-possible-to-simulate-keyboard-mouse-event-in-nodejs) – Venryx Sep 08 '20 at 15:48

2 Answers2

6

I know this thread is old, but you can use Nut.js.

Minimal Example:

const { keyboard } = require('@nut-tree/nut-js');
keyboard.config.autoDelayMs = 0;

   keyboard.type("Hello World!");

I personally think that RobotJS uses too many dependencies. Nut.js is much easier to use and much faster to set up.

MiCROZi
  • 84
  • 1
  • 6
1

I think RobotJS may help you, it allows you to automate various desktop actions programatically.

Here's an example:

// Require RobotJS
var robot = require('robotjs')
// Tap space key
robot.keyTap("space")
theob
  • 199
  • 1
  • 10