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

风流意气都作罢 提交于 2019-12-21 05:22:25

问题


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:

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.

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

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.

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.

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

Is there any way to do this?


回答1:


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




回答2:


I made a node module to do this too: 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);



回答3:


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.

Install it with 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://stackoverflow.com/questions/21848747/node-js-javascript-simulate-keypress-on-server-like-a-macro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!