javascript 是否可以在 NodeJS 中模拟键盘/鼠标事件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11178372/
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
Is it possible to simulate keyboard/mouse event in NodeJS?
提问by jayarjo
Imagine that a NodeJS module, when invoked from console, outputs some introductory messages and then waits for user input (click enter or esc). This module already has and does everything we require, except that - wait-for-user-input prompt. So we wonder (I'm personally very new to NodeJS) if it is possible to execute console module programmatically and trigger an input event on it, so that it doesn't wait and proceed with the job right away?
想象一下,当从控制台调用时,NodeJS 模块会输出一些介绍性消息,然后等待用户输入(单击 Enter 或 esc)。这个模块已经拥有并完成了我们需要的一切,除了 - 等待用户输入提示。所以我们想知道(我个人对 NodeJS 很陌生)是否可以以编程方式执行控制台模块并在其上触发输入事件,这样它就不会等待并立即继续工作?
回答by Jason Stallings
回答by JerryGoyal
As Jason mentioned you could use RobotJSfor key simulation but there are couple of steps require to correctly build robotJS for Windowspaltform:
正如 Jason 提到的,您可以使用RobotJS进行关键模拟,但要为Windowspaltform正确构建 RobotJS,需要几个步骤:
- You would need windows build tools so run
npm install --global windows-build-tools
(would take some time as it's around 120MB) - run
npm install robotjs --save-dev
You're done!.
If this is for electronapp then you would also require below 3rd step: run
npm rebuild --runtime=electron --target=1.7.9 --disturl=https://atom.io/download/atom-shell --abi=57
(1.7.9 is my
electron --version
and abi is for my correspondingnode --version
8.7 installed, you can check abi version for node version here[look for NODE_MODULE_VERSION column])
- 您需要 Windows 构建工具才能运行
npm install --global windows-build-tools
(需要一些时间,因为它大约 120MB) - 运行
npm install robotjs --save-dev
你完成了!
如果这是用于电子应用程序,那么您还需要以下第 3 步: 跑
npm rebuild --runtime=electron --target=1.7.9 --disturl=https://atom.io/download/atom-shell --abi=57
(1.7.9 是我的
electron --version
,abi 是我对应的node --version
8.7 安装的,你可以在这里查看节点版本的 abi 版本[查找 NODE_MODULE_VERSION 列])
回答by computeiro
node-key-sender library is an alternative to RobotJs if you just need to send keys to your operational system. It is cross platform and very small lib.
如果您只需要将密钥发送到您的操作系统,则 node-key-sender 库是 RobotJs 的替代方案。它是跨平台和非常小的库。
Install it with npm install --save-dev node-key-sender
.
安装它npm install --save-dev node-key-sender
。
And send "enter" to the keyboard using:
并使用以下命令将“输入”发送到键盘:
var ks = require('node-key-sender');
ks.sendKey('enter');
Check out the documentation page: https://www.npmjs.com/package/node-key-sender.