admin管理员组

文章数量:1318573

I am coding a twitch bot and I want it so when users type a mand (something like !space [time] ) it will randomly press space for the ammount of time they gave. I know how to receive the mand 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

I am coding a twitch bot and I want it so when users type a mand (something like !space [time] ) it will randomly press space for the ammount of time they gave. I know how to receive the mand 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

Share Improve this question edited Jun 16, 2018 at 10:21 Kong asked Jun 16, 2018 at 10:07 KongKong 351 gold badge1 silver badge7 bronze badges 5
  • 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 Commented Jun 16, 2018 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 Commented Jun 16, 2018 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 xyproblem.info – Quentin Commented Jun 16, 2018 at 10:21
  • Added more details – Kong Commented Jun 16, 2018 at 10:23
  • Does this answer your question? Is it possible to simulate keyboard/mouse event in NodeJS? – Venryx Commented Sep 8, 2020 at 15:48
Add a ment  | 

2 Answers 2

Reset to default 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.

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

本文标签: javascriptHow to send keypresses through nodejsStack Overflow