Robotjs 桌面自动化
最后更新于:2022-04-02 03:30:57
[TOC]
> [官网](https://robotjs.io/docs/)
## 概述
Robotjs是nodejs的第一个用于桌面自动化的库。他能自动化鼠标、键盘和读取屏幕,并且提供了Mac, Windows, and Linux的跨平台支持
## 安装
`npm install robotjs`
## 示例
## [Mouse](https://robotjs.io/docs/syntax#mouse)
```
// Move the mouse across the screen as a sine wave.
var robot = require("robotjs");
// Speed up the mouse.
robot.setMouseDelay(2);
var twoPI = Math.PI * 2.0;
var screenSize = robot.getScreenSize();
var height = (screenSize.height / 2) - 10;
var width = screenSize.width;
for (var x = 0; x < width; x++)
{
y = height * Math.sin((twoPI * x) / width) + height;
robot.moveMouse(x, y);
}
```
## [Keyboard](https://robotjs.io/docs/syntax#keyboard)
```
// Type "Hello World" then press enter.
var robot = require("robotjs");
// Type "Hello World".
robot.typeString("Hello World");
// Press enter.
robot.keyTap("enter");
```
## [Screen](https://robotjs.io/docs/syntax#screen)
```
// Get pixel color under the mouse.
var robot = require("robotjs");
// Get mouse position.
var mouse = robot.getMousePos();
// Get pixel color in hex format.
var hex = robot.getPixelColor(mouse.x, mouse.y);
console.log("#" + hex + " at x:" + mouse.x + " y:" + mouse.y);
```
';