Node.js JavaScript:在服务器上模拟按键(就像一个宏)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21848747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 21:49:19  来源:igfitidea点击:

Node.js JavaScript: Simulate Keypress on Server (Like a Macro)

javascriptnode.jsirc

提问by Nimaid

I am trying to get a node.js script to simulate a keypress, such as the up arrow or the a button. Specifically, I am trying to make a clone of Twitch Plays Pokemon. Basically, whenever a command (up, down, left, right, a, b, select, start) is sent via IRC, the server simulates a keypress, which in turn controls a gameboy emulator. So far, I have written this with the IRC module for node.js:

我正在尝试使用 node.js 脚本来模拟按键,例如向上箭头或 a 按钮。具体来说,我正在尝试制作Twitch Plays Pokemon的克隆。基本上,每当通过 IRC 发送命令(向上、向下、向左、向右、a、b、选择、开始)时,服务器都会模拟按键,进而控制游戏机模拟器。到目前为止,我已经用 node.js 的 IRC 模块编写了这个:

var config = {
    channels: ["#tron"],
    server: "irc.freenode.net",
    botName: "wyatt"
};

var irc = require("irc");

var bot = new irc.Client(config.server, config.botName, {
    channels: config.channels
});

var commandHandler = function(from, text) {
    if(text.toLowerCase() === "up"||text.toLowerCase() === "down"||text.toLowerCase() === "left"||text.toLowerCase() === "right"||text.toLowerCase() === "a"||text.toLowerCase() === "b"||text.toLowerCase() === "select"||text.toLowerCase() === "start") {
        bot.say(config.channels[0], from.toUpperCase() + " sent the " + text.toUpperCase() + " command!");
    } else {
        bot.say(config.channels[0], from.toUpperCase() + ", that wasn't a valid command!");
    }
};

bot.addListener("message", function(from, to, text, message) {
    commandHandler(from, text);
});

To run my script, I type node scriptName.js into a command prompt. I am using Windows 7.

为了运行我的脚本,我在命令提示符中键入 node scriptName.js。我正在使用 Windows 7。

This connects to the freenodechannel #tron, which I am using for testing purposes, as it seems to be mainly dormant.

这连接到freenode频道#tron,我将其用于测试目的,因为它似乎主要处于休眠状态。

When a user inputs one of the accpted commands, it sends a message like "NIMAID sent the LEFT command!", otherwise it sends "NIMAID, that wasn't a valid command!". As it is, it works flawlessly. So all I need to do is find a way to send a keypress and the final script is just a switch statement away.

当用户输入其中一个已接受的命令时,它会发送一条消息,例如“NIMAID 发送了 LEFT 命令!”,否则它会发送“NIMAID,那不是有效命令!”。事实上,它完美无缺。所以我需要做的就是找到一种发送按键的方法,而最终的脚本只是一个 switch 语句。

The trouble is that any references I can find by searching the internet talks about using node.js in a browser environment, with JQuery or something similar. I need to send keypresses to an emulator.

问题是,我可以通过在互联网上搜索有关在浏览器环境中使用 node.js,使用 JQuery 或类似的东西找到的任何参考资料。我需要将按键发送到模拟器。

tldr: I need to send keypresses from a node.js script to an application running on the windows 7 server desktop.

tldr:我需要将按键从 node.js 脚本发送到在 Windows 7 服务器桌面上运行的应用程序。

Is there any way to do this?

有没有办法做到这一点?

回答by Kyle Paulsen

I made a node module to do this too: https://github.com/kylepaulsen/kbm-robot

我也做了一个节点模块来做到这一点:https: //github.com/kylepaulsen/kbm-robot

var robot = require("kbm-robot");

robot.startJar();

robot.press("alt")
    .press("tab")
    .sleep(100)
    .release("tab")
    .release("alt")
    .sleep(100)
    .typeString("Hello World!")
    .go()
    .then(robot.stopJar);

回答by Stuart P. Bentley

Apparently, there's a win_keyboardmodule in the npm registry that somebody wrote to control the keyboard in Windows. You can run npm install win_keyboardand use that; it appears to do exactly what you want.

显然,有人编写的 npm 注册表中有一个win_keyboard模块来控制 Windows 中的键盘。你可以运行npm install win_keyboard并使用它;它似乎完全符合您的要求。

回答by computeiro

You may try an alternative to RobotJS. It is a very small and still cross platform library to send keys to your operational system called node-key-sender. I developed after getting frustrated with RobotJS and kbm-robot.

您可以尝试使用 RobotJS 的替代方法。它是一个非常小且仍然跨平台的库,用于将密钥发送到名为 node-key-sender 的操作系统。在对 RobotJS 和 kbm-robot 感到沮丧后,我开发了它。

Install it with npm install --save-dev node-key-sender.

安装它npm install --save-dev node-key-sender

And send a text to the keyboard using:

并使用以下命令向键盘发送文本:

var ks = require('node-key-sender');
ks.sendText('This is my text');

Check out the documentation page: https://www.npmjs.com/package/node-key-sender.

查看文档页面:https: //www.npmjs.com/package/node-key-sender